I wrote the following intending to create an array of 3 element, each
being itself an array of two elements initialized to zero; Judging by
"inspect", I seem to have that:
On Mon, Aug 25, 2008 at 6:55 PM, RichardOnRails <RichardDummyMailbox58407@uscomputergurus.com> wrote:
Hi,
I wrote the following intending to create an array of 3 element, each
being itself an array of two elements initialized to zero; Judging by
"inspect", I seem to have that:
This is a gotcha that trips up everyone the first time they come
across it. Array.new(size, obj) creates a new array with _size_ copies
of _obj_. That is, your array has three copies of the array [0,0].
Each one is the same as the other. So when you assign to the first
element of that array, all copies get updated. You'll want to use
Array.new with a block to get different objects:
On Aug 25, 4:57 pm, RichardOnRails <RichardDummyMailbox58...@uscomputergurus.com> wrote:
Hi,
I wrote the following intending to create an array of 3 element, each
being itself an array of two elements initialized to zero; Judging by
"inspect", I seem to have that:
Many thanks.
Thanks to Michael for being first with a quick solution
Thanks to Todd for suggesting what might be more elegant in certain
circumstances.
Thanks to Chris for revealing exact how Ruby handles my construct and
the one I really want.
Best wishes,
Richard
···
On Aug 25, 6:57 pm, RichardOnRails <RichardDummyMailbox58...@uscomputergurus.com> wrote:
Hi,
I wrote the following intending to create an array of 3 element, each
being itself an array of two elements initialized to zero; Judging by
"inspect", I seem to have that:
You might want to have a look at the post Ruby (Al2O3::Cr) describing nicely
some details about pointers in Ruby. Might be a good addition to what
has already been said here.
Why would someone do it this way? Because you can change what's
inside the new() instead of steadfastly saying it must be [0, 0].
Sort of moving data away from logic a little. Easier to debug, blah,
blah...
If that's not really a big deal, I would go Michael's and Chris's route.
Todd
···
On Mon, Aug 25, 2008 at 6:07 PM, Michael Guterl <mguterl@gmail.com> wrote:
On Mon, Aug 25, 2008 at 6:55 PM, RichardOnRails > <RichardDummyMailbox58407@uscomputergurus.com> wrote:
Hi,
I wrote the following intending to create an array of 3 element, each
being itself an array of two elements initialized to zero; Judging by
"inspect", I seem to have that:
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 Aug 26, 8:26 am, "Thomas B." <tpr...@gmail.com> wrote:
on a septuagenarian But I have a more substantive question. The site says:
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.
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.
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. But
there is a method to check it: equal?. It works like == in Java.
a=[whatever]
a.equal?(a) #=> true # the very same Array instance
a.equal?(a.dup) #=> false # it's another instance, only it looks like
the old one when inspect'ed
But still the whatever inside a and a.dup is exactly the same thing:
a[0].euqal?(a.dup[0]) #=> true
That proves the copy is shallow - there's a new object, but it holds old
references.
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:
[#<Object:0x2842310>], 21107090
[#<Object:0x2842310>], 21107070
-Adam
···
On 8/28/08, Thomas B. <tpreal@gmail.com> 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.
Thanks for example. My mistake was thinking that a.inspect, which
yielded [#<Object:0x2842310>] 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?
On Aug 28, 1:07 pm, Adam Shelly <adam.she...@gmail.com> wrote:
On 8/28/08, Thomas B. <tpr...@gmail.com> 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: [#<Object:0x2808138>]; #<Object:0x2808138>; Array
> > 4: [#<Object:0x2808138>]; #<Object:0x2808138>; Array
> The [#<Object:0x2808138>]'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:
[#<Object:0x2842310>], 21107090
[#<Object:0x2842310>], 21107070