On Aug 26, 8:26 am, "Thomas B." <tpr...@gmail.com> wrote:
> RichardOnRails wrote:
> > a[0][0]=5 => [[5, 0], [5, 0], [5, 0]]
>
> You might want to have a look at the posthttp://al2o3-cr.blogspot.com/2008/08/object-arr.htmldescribing nicely
> some details about pointers in Ruby. Might be a good addition to what
> has already been said here.
> --
> Posted viahttp://www.ruby-forum.com/.
Hi Thomas,
Thanks for the link, which offers a lot of interesting things. On
aesthetic grounds, I'd like a white background. The black is tough
on a septuagenarian
But I have a more substantive question. The site says:
<quote>
[x] #=> [#<Object:0x2d8ce10>]
[x].dup #=> [#<Object:0x2d8ce10>]
As you see,
dup on an object makes a new object (the addresses differ) [referring
to earlier statements], but
dup on an array makes a new array but does not make a copy of the
elements - it just makes a new array with its elements pointing to the
original objects.
</quote>
My minor exception is that an array IS an object, so the two
paragraphs sound weird. He is contrasting an object of class Object
to an object of class Array.
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.
I am missing something?
Best wishes,
Richard
Code
====
x = Object.new
puts "1: " + x.inspect
puts "2: " + x.dup.inspect
a = [x]
ad = a.dup
puts "3: " + a.inspect+ "; " + a[0].inspect + "; " + a.class.to_s
puts "4: " + ad.inspect + "; " + ad[0].inspect + "; " + ad.class.to_s
Output
======
1: #<Object:0x2808138>
2: #<Object:0x28080c0>
3: [#<Object:0x2808138>]; #<Object:0x2808138>; Array
4: [#<Object:0x2808138>]; #<Object:0x2808138>; Array