On Thursday 28 Aug 2003 4:48 pm, Hal Fulton wrote:
> Hello, all...
>
> I'm wondering what should be the behavior of collect when the block
> modifies the receiver.
>
> I don't recall thinking about this before, but I would have said
> that the collect always returns as many items as the array had
> at the time of the call.
>
> Has this somehow changed since 1.6.*?
>
> The reason I ask is the email (below) I got from Jonathan Lim.
> The randomize methods on page 152/153 of _The Ruby Way_ do not
> work. It's possible they never did, but I find that hard to
> believe.
>
> I don't have an old Ruby around, or I'd test it.
>

Running the code from The Ruby Way I get

[mark@laptop mark]$ ruby1.6 -v test.rb
ruby 1.6.7 (2002-03-01) [i586-linux-gnu]
[1, 4, 3, 5, 2]
[4, 5, 2, 3, 1]
[mark@laptop mark]$ ruby -v test.rb
ruby 1.8.0 (2003-08-04) [i686-linux]
test.rb:5: warning: parenthesize argument(s) for future version
test.rb:10: warning: parenthesize argument(s) for future version
[2, 5, 1]
[2, 3, 4]

So it does look like there has been a change between 1.6.7 and 1.8.0

After changing 

arr.collect { arr.slice!(rand arr.length) }

to

self.collect { arr.slice!(rand arr.length) }

I get

[mark@laptop mark]$ ruby1.6 -v test.rb
ruby 1.6.7 (2002-03-01) [i586-linux-gnu]
[5, 3, 4, 2, 1]
[2, 1, 3, 5, 4]
[mark@laptop mark]$ ruby -v test.rb
ruby 1.8.0 (2003-08-04) [i686-linux]
[5, 3, 1, 2, 4]
[5, 2, 1, 4, 3]

So this does fix the problem.

Best regards

Mark Sparshatt