From: RichardOnRails Date: 2008-08-29T03:30:17+09:00 Subject: Re: Dumb "array of arrays" question On Aug 28, 1:07 pm, Adam Shelly wrote: > On 8/28/08, Thomas B. wrote: > > > > > Hello Richard, > > > RichardOnRails wrote: > > > More importantly, according to my tests below, dup'ing an array does > > > NOT make a new array. It returns the (address of/pointer to) the same > > > old array. > > > dup'ing an array DOES make a new array, only the new array looks like > > the old one, because Array#inspect doesn't return the address of the > > array so you cannot see if it is exactly the same instance or not. > > > 3: [#]; #; Array > > > 4: [#]; #; Array > > > The [#]'s here are in fact different objects, only > > they look the same. > > You can prove they are different objects this way: > code: > a = [Object.new]; ad = a.dup > puts a.inspect+ ", #{a.object_id}" > puts ad.inspect+ ", #{ad.object_id}" > output: > [#], 21107090 > [#], 21107070 > > -Adam Hi Adam, Thanks for example. My mistake was thinking that a.inspect, which yielded [#] was yielding the a's object_id. I speculated that inspect yielded its receiver's content's object_id. That's wrong, as evidenced by the following code. So is inspect returning in this instance? Best wishes, Richard a = [x] ad = a.dup puts "1: " + a.object_id.to_s+ "; " + a[0].object_id.to_s + "; " + a.inspect puts "2: " + ad.object_id.to_s + "; " + ad[0].object_id.to_s + "; " + ad.inspect 1: 20987380; 20987470; [#] 2: 20987370; 20987470; [#]