From: Randy Kramer Date: 2008-09-20T10:00:24+09:00 Subject: Re: why one array continues to grow after repeated call On Friday 19 September 2008 03:19 pm, Brian Candler wrote: > 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 -- I didn't have time to write a short letter, so I created a video instead.--with apologies to Cicero, et.al.