On Fri, Dec 5, 2008 at 10:20 AM, Hamza Haiken <tenkage / gmail.com> wrote:
> Hi
>
> Can someone explain to me why is this happening ?
x = [42] makes x refer to the ArrayObject
y = x makes y refer to the *same* object, see for yourself
irb(main):001:0> x=[42]
=> [42]
irb(main):002:0> p x.object_id
77740410
=> 77740410
irb(main):003:0> y = x
=> [42]
irb(main):004:0> p y.object_id
77740410
=> 77740410

Now if you do this
irb(main):005:0> x << 42
=> [42, 42]

guess what y will refer to, still the same object, that happens to
have been altered

irb(main):006:0> y
=> [42, 42]

If you want y to refer to a different object you can do this
irb(main):001:0> x = [42]
=> [42]
irb(main):002:0> y = x.dup
=> [42]
irb(main):003:0> p [x.object_id, y.object_id]
[77455250, 77432630]
=> [77455250, 77432630]
irb(main):004:0> x << 42
=> [42, 42]
irb(main):005:0> p [x, y]
[[42, 42], [42]]
=> [[42, 42], [42]]

HTH
R.


-- 
Ne baisse jamais la tů’e, tu ne verrais plus les Ů’oiles.

Robert Dober ;)