Here's a case where it does:
# version 1
data =
10.times { |i| data[i] = "hello" }
data[0] << "x"
p data# version 2
str = "hello"
data =
10.times { |i| data[i] = str }
data[0] << "x"
p data
Wow! That's either amazing or frightening (or both).
I did use:
10.times { |i| puts data[i].object_id}
... to see the object_id of each element in the arrays both above and
below.
(For the record, version 1 creates 10 different objects, version 2
creates only a single object.)
It does catch people out when they write, e.g.
a = Array.new(10, )
when what they really want is
a = Array.new(10) { }
Try both these examples, printing out the object_id of each of the 10
elements of the array, to see how they are different.
Thanks again (I think
Randy Kramer
···
On Friday 19 September 2008 03:19 pm, Brian Candler wrote:
--
I didn't have time to write a short letter, so I created a video
instead.--with apologies to Cicero, et.al.