# Simple newbie question

**URL:** <https://www.ruby-forum.com/t/simple-newbie-question/53129>\
**Category:** Ruby\
**Created:** [January 26, 2006, 2:58am UTC](https://www.ruby-forum.com/t/simple-newbie-question/53129 "2006-01-26T02:58:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kieran\_kirwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/8dc957/32.png) [@kieran\_kirwan](https://www.ruby-forum.com/u/kieran_kirwan)\
**Post date:** [January 26, 2006, 2:58am UTC](https://www.ruby-forum.com/t/simple-newbie-question/53129/1 "2006-01-26T02:58:02Z")

</div>

Hi,  
I have a piece of code below in which I;  
1…make a new NumberList object and append two MyNumber objects.  
2…copy the NumberList object and append a further MyNumber object.  
However after this both objects are the same.  
Any help would be greatly appreciated.  
Thanks,  
Kieran

* * *

class MyNumber  
def initialize(x)  
@x=x  
end  
attr\_accessor :x  
end  
class MyNumberList  
def initialize  
@nums = Array.new  
end  
def append(num)  
@nums.push(num)  
self  
end  
def sum  
sum=0  
@nums.each {|s| sum += s.x }  
return sum  
end  
end

zl1=MyNumberList.new; zl1.append(MyNumber.new(10));  
zl1.append(MyNumber.new(20))  
puts zl1.sum  
zl2=zl1; zl2.append(MyNumber.new(40))  
puts zl1.sum  
puts zl2.sum

---

<div class="post-metadata">

**Author:** ![kieran\_kirwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/8dc957/32.png) [@kieran\_kirwan](https://www.ruby-forum.com/u/kieran_kirwan)\
**Post date:** [January 26, 2006, 3:16am UTC](https://www.ruby-forum.com/t/simple-newbie-question/53129/2 "2006-01-26T03:16:24Z")

</div>

Hi –

On Thu, 26 Jan 2006, kieran kirwan wrote:

> Hi,  
> I have a piece of code below in which I;  
> 1…make a new NumberList object and append two MyNumber objects.  
> 2…copy the NumberList object and append a further MyNumber object.  
> However after this both objects are the same.  
> […]  
> zl2=zl1

That’s why they’re the same: by doing this assignment, you’re storing  
a new reference to the object in zl2. But it’s still the same object.

David

–  
David A. Black  
removed\_email\_address@domain.invalid

“Ruby for Rails”, from Manning Publications, coming May 1, 2006!

> **[Ruby for Rails](https://www.manning.com/books/ruby-for-rails)**
>
> NEWER EDITION AVAILABLE
> 
> The Well-Grounded Rubyist, Second Edition is now available. An eBook of the previous edition, The Well-Grounded Rubyist is included at no additional cost when you buy the revised edition! 
> 
> 
> Ruby for Rails helps Rails...

---

<div class="post-metadata">

**Author:** ![kieran\_kirwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/8dc957/32.png) [@kieran\_kirwan](https://www.ruby-forum.com/u/kieran_kirwan)\
**Post date:** [January 26, 2006, 1:28pm UTC](https://www.ruby-forum.com/t/simple-newbie-question/53129/3 "2006-01-26T13:28:31Z")

</div>

kieran kirwan [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> Hi,  
> I have a piece of code below in which I;  
> 1…make a new NumberList object and append two MyNumber objects.  
> 2…copy the NumberList object and append a further MyNumber object.  
> However after this both objects are the same.  
> Any help would be greatly appreciated.

See [http://www.rubygarden.org/ruby?Make\_A\_Deep\_Copy\_Of\_An\_Object](http://www.rubygarden.org/ruby?Make_A_Deep_Copy_Of_An_Object)

> zl2=zl1;

As said, you doesn’t copy the contents of zl1 in zl2: you make zl2 and  
zl1 reference the same objet. What you want to do is to copy the  
_contents_ of zl1 into zl2.
