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 =3D [42] makes x refer to the ArrayObject
y =3D x makes y refer to the *same* object, see for yourself
irb(main):001:0> x=3D[42]
=3D> [42]
irb(main):002:0> p x.object_id
77740410
=3D> 77740410
irb(main):003:0> y =3D x
=3D> [42]
irb(main):004:0> p y.object_id
77740410
=3D> 77740410

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

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

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

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

HTH
R.


--=20
Ne baisse jamais la t=EAte, tu ne verrais plus les =E9toiles.

Robert Dober ;)