Array#shift weird problem

H

Hamza Haiken

Hi

Can someone explain to me why is this happening ?
matrix1 = ["One","Two","Three"] => ["One", "Two", "Three"]
matrix2 = matrix1 => ["One", "Two", "Three"]
matrix2.shift => "One"
matrix2 => ["Two", "Three"]
matrix1
=> ["Two", "Three"]

According to my logic, when I do matrix2.shift, it should have nothing
to do with matrix1. Why's this one shifted as well ?
 
S

Stefano Crocco

Alle Friday 05 December 2008, Hamza Haiken ha scritto:
Hi

Can someone explain to me why is this happening ?
matrix1 = ["One","Two","Three"]

=> ["One", "Two", "Three"]

=> ["One", "Two", "Three"]

=> "One"

=> ["Two", "Three"]

=> ["Two", "Three"]

According to my logic, when I do matrix2.shift, it should have nothing
to do with matrix1. Why's this one shifted as well ?

Because matrix1 and matrix2 are only different names used to refer to the same
thing, as you can see by checking the values returned by matrix1.object_id and
matrix2.object_id. If you want the two variables to contain different objects,
you need to write

matrix2 = matrix1.dup

This will set the contents of matrix2 to a duplicate of matrix1, so that
changing one won't change the other. Note, however, that this is only a
shallow copy, that is: the array itself is duplicated, but its contents
aren't. This means that any method which changes one of the contained strings
in-place will affect both array. For example:

matrix2 = matrix1.dup
matrix1[0].sub!('O','o')
p matrix1
#=> ["one", "Two", "Three"]
p matrix2
#=> ["one", "Two", "Three"]

If you also need to be sure that the arrays contain different objects, you
need to do a deep copy, which can be achieved using marshal:

matrix2 = Marshal.load(Marshal.dump(matrix1))

I hope this helps

Stefano
 
P

Peña, Botp

RnJvbTogSGFtemEgSGFpa2VuIFttYWlsdG86dGVua2FnZUBnbWFpbC5jb21dIA0KIyBDYW4gc29t
ZW9uZSBleHBsYWluIHRvIG1lIHdoeSBpcyB0aGlzIGhhcHBlbmluZyA/DQoNCmhlcmUgbWF0cml4
MSBob2xkcyBhIHB0ci9yZWZlcmVuY2UgdG8gdGhlIGFycmF5IG9iamVjdA0KDQojID4+IG1hdHJp
eDEgPSBbIk9uZSIsIlR3byIsIlRocmVlIl0NCiMgPT4gWyJPbmUiLCAiVHdvIiwgIlRocmVlIl0N
Cg0KaGVyZSB0aGUgcmVmZXJlbmNlIGlzIGFsc28gY29waWVkIHRvIG1hdHJpeDINCg0KIyA+PiBt
YXRyaXgyID0gbWF0cml4MQ0KIyA9PiBbIk9uZSIsICJUd28iLCAiVGhyZWUiXQ0KDQo=
 
R

Robert Dober

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 ;)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,184
Messages
2,570,978
Members
47,578
Latest member
LC_06

Latest Threads

Top