H
Hal Fulton
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.
The original line in randomize, of course, read as follows:
arr.collect { arr.slice!(rand arr.length) }
Thanks to you who reply, and thanks again to Jonathan.
Cheers,
Hal
-------- Original Message --------
Subject: Randomizing an Array Errata?
Date: Thu, 28 Aug 2003 15:12:45 +0100
From: Jonathan Lim <[email protected]>
To: (e-mail address removed)
Hi,
On p.153 of the Ruby Way
Should it not read this?
def randomize
arr = self.dup
self.collect { arr.slice!(rand arr.length) }
end
def randomize!
arr = self.dup
result = self.collect { arr.slice!(rand arr.length) }
self.replace result
end
Cheers,
Jon
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.
The original line in randomize, of course, read as follows:
arr.collect { arr.slice!(rand arr.length) }
Thanks to you who reply, and thanks again to Jonathan.
Cheers,
Hal
-------- Original Message --------
Subject: Randomizing an Array Errata?
Date: Thu, 28 Aug 2003 15:12:45 +0100
From: Jonathan Lim <[email protected]>
To: (e-mail address removed)
Hi,
On p.153 of the Ruby Way
Should it not read this?
def randomize
arr = self.dup
self.collect { arr.slice!(rand arr.length) }
end
def randomize!
arr = self.dup
result = self.collect { arr.slice!(rand arr.length) }
self.replace result
end
Cheers,
Jon