# Why this behaviour?

**URL:** <https://www.ruby-forum.com/t/why-this-behaviour/125485>\
**Category:** Ruby\
**Created:** [January 9, 2008, 3:35pm UTC](https://www.ruby-forum.com/t/why-this-behaviour/125485 "2008-01-09T15:35:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![murena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7ba0ec/32.png) [@murena](https://www.ruby-forum.com/u/murena)\
**Post date:** [January 9, 2008, 3:35pm UTC](https://www.ruby-forum.com/t/why-this-behaviour/125485/1 "2008-01-09T15:35:59Z")

</div>

Hello to all,

i can’t understand the follow behaviour:

class Player  
attr\_accessor :hand  
def initialize(hand)  
@hand = hand  
end  
end

def roba(player)  
p2 = Player.new(player.hand)  
change p2  
print "p2 = "  
p p2  
print "player = "  
p player  
end

def change(player)  
player.hand.push(3)  
end

p1 = Player.new([2, 3, 4])  
roba p1

**END**

output is:  
p2 = #\<Player:0xb7c4919c @hand=[2, 3, 4, 3]\>  
player = #\<Player:0xb7c491c4 @hand=[2, 3, 4, 3]\>

Why the change affects both objects? I want to copy the first object and  
work on the copy without affecting the original.

Thanks

---

<div class="post-metadata">

**Author:** ![murena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7ba0ec/32.png) [@murena](https://www.ruby-forum.com/u/murena)\
**Post date:** [January 9, 2008, 3:49pm UTC](https://www.ruby-forum.com/t/why-this-behaviour/125485/2 "2008-01-09T15:49:24Z")

</div>

On Jan 9, 2008 3:35 PM, Daniele A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hello to all,
> 
> i can’t understand the follow behaviour:
> 
> class Player  
> attr\_accessor :hand  
> def initialize(hand)

This copies reference to the array, use #dup to create a copy (note  
that it’s not recursive):

- 

```
 @hand = hand

```

- 

```
@hand = hand.dup

```

---

<div class="post-metadata">

**Author:** ![murena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7ba0ec/32.png) [@murena](https://www.ruby-forum.com/u/murena)\
**Post date:** [January 9, 2008, 3:51pm UTC](https://www.ruby-forum.com/t/why-this-behaviour/125485/3 "2008-01-09T15:51:09Z")

</div>

On Jan 9, 9:35 am, Daniele A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Why the change affects both objects? I want to copy the first object and  
> work on the copy without affecting the original.

Both Player objects contain a reference to the same array. What you  
want is this:

class Player  
attr\_accessor :hand  
def initialize(hand)  
@hand = hand.clone  
end  
end

---

<div class="post-metadata">

**Author:** ![murena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7ba0ec/32.png) [@murena](https://www.ruby-forum.com/u/murena)\
**Post date:** [January 9, 2008, 3:52pm UTC](https://www.ruby-forum.com/t/why-this-behaviour/125485/4 "2008-01-09T15:52:55Z")

</div>

Daniele A. wrote:

> I want to copy the first object and  
> work on the copy without affecting the original.

Then do that. dup copies an object. Assignment does _not_ copy.

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![murena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7ba0ec/32.png) [@murena](https://www.ruby-forum.com/u/murena)\
**Post date:** [January 9, 2008, 3:54pm UTC](https://www.ruby-forum.com/t/why-this-behaviour/125485/5 "2008-01-09T15:54:06Z")

</div>

On Jan 9, 2008 3:35 PM, Daniele A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> player.hand.push(3)  
> player = #\<Player:0xb7c491c4 @hand=[2, 3, 4, 3]\>
> 
> Why the change affects both objects?

Because both players point to the same array object  
in their @hand variables. To see what I mean try this:

irb(main):028:0\> p = Player.new([2,3,4])  
=\> #\<Player:0xb7c16044 @hand=[2, 3, 4]\>  
irb(main):029:0\> p.hand.object\_id  
=\> -606031798  
irb(main):030:0\> p2 = Player.new(p.hand)  
=\> #\<Player:0xb7c00b40 @hand=[2, 3, 4]\>  
irb(main):031:0\> p2.hand.object\_id  
=\> -606031798

So when you push an element in the array,  
both hands point to the same array and you see  
the change in both player objects.

> I want to copy the first object and  
> work on the copy without affecting the original.

You can dup the array in the initialize:

def initialize(hand)  
@hand = hand.dup  
end

Hope this helps,

Jesus.
