# Array#shift weird problem

**URL:** <https://www.ruby-forum.com/t/array-shift-weird-problem/154937>\
**Category:** Ruby\
**Created:** [December 5, 2008, 10:26am UTC](https://www.ruby-forum.com/t/array-shift-weird-problem/154937 "2008-12-05T10:26:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tenchi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9e5f3/32.png) [@tenchi](https://www.ruby-forum.com/u/tenchi)\
**Post date:** [December 5, 2008, 10:26am UTC](https://www.ruby-forum.com/t/array-shift-weird-problem/154937/1 "2008-12-05T10:26:38Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![tenchi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9e5f3/32.png) [@tenchi](https://www.ruby-forum.com/u/tenchi)\
**Post date:** [December 5, 2008, 10:41am UTC](https://www.ruby-forum.com/t/array-shift-weird-problem/154937/2 "2008-12-05T10:41:23Z")

</div>

Alle Friday 05 December 2008, Hamza H. ha scritto:

> =\> [“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

---

<div class="post-metadata">

**Author:** ![tenchi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9e5f3/32.png) [@tenchi](https://www.ruby-forum.com/u/tenchi)\
**Post date:** [December 5, 2008, 10:47am UTC](https://www.ruby-forum.com/t/array-shift-weird-problem/154937/3 "2008-12-05T10:47:15Z")

</div>

On Fri, Dec 5, 2008 at 10:20 AM, Hamza H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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ête, tu ne verrais plus les étoiles.

Robert D. 😉

---

<div class="post-metadata">

**Author:** ![tenchi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9e5f3/32.png) [@tenchi](https://www.ruby-forum.com/u/tenchi)\
**Post date:** [December 5, 2008, 10:42am UTC](https://www.ruby-forum.com/t/array-shift-weird-problem/154937/4 "2008-12-05T10:42:34Z")

</div>

From: Hamza H. [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# Can someone explain to me why is this happening ?

here matrix1 holds a ptr/reference to the array object

# \>\> matrix1 = [“One”,“Two”,“Three”]

# =\> [“One”, “Two”, “Three”]

here the reference is also copied to matrix2

# \>\> matrix2 = matrix1

# =\> [“One”, “Two”, “Three”]
