# Misunderstanding the Passing of references

**URL:** <https://www.ruby-forum.com/t/misunderstanding-the-passing-of-references/98699>\
**Category:** Ruby\
**Created:** [January 30, 2006, 7:13am UTC](https://www.ruby-forum.com/t/misunderstanding-the-passing-of-references/98699 "2006-01-30T07:13:03Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![sporkyy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c89c15/32.png) [@sporkyy](https://www.ruby-forum.com/u/sporkyy)\
**Post date:** [January 30, 2006, 7:13am UTC](https://www.ruby-forum.com/t/misunderstanding-the-passing-of-references/98699/1 "2006-01-30T07:13:03Z")

</div>

I’m to to ruby…

Currently, I’m stuck in a situation where I’m changing values I’m not  
intending to and I don’t know how to correct it.

I have… A class “Model” and a class “DataObject”. The “Data Object”  
class has a method which takes in a “Model” and returns a new “Model”.  
The problem is, my “generate\_new\_model” method is altering the input  
“Model” object.

The below show basically what I’ve done to create the objects. Somehow  
when I define a new “Model” for output and set it’s values to the source  
model’s values, when I change the outputModel values it alters the  
source model.

What am I doing wrong?

@myModel = Model.new  
@myData = DataObject.new

newModel = @myData.generate\_new\_model(@myModel)

def generate\_new\_model(modelSource)  
outputModel = Model.new  
outputModel = modelSource

```
outputModel.values = "new values"

outputModel

```

end

---

<div class="post-metadata">

**Author:** ![sporkyy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c89c15/32.png) [@sporkyy](https://www.ruby-forum.com/u/sporkyy)\
**Post date:** [January 30, 2006, 7:31am UTC](https://www.ruby-forum.com/t/misunderstanding-the-passing-of-references/98699/2 "2006-01-30T07:31:04Z")

</div>

> @myModel = Model.new  
> @myData = DataObject.new
> 
> newModel = @myData.generate\_new\_model(@myModel)
> 
> def generate\_new\_model(modelSource)  
> outputModel = Model.new

So far, so good. At the minute, outputModel references a brand new  
Model.

> ```
> outputModel = modelSource
> 
> ```

Now, however, it references the same model as modelSource. Oops.

> ```
> outputModel.values = "new values"
> 
> ```

And here you’ve changed the values of the Model referenced by  
outputModel and modelSource.

> ```
> outputModel
> 
> ```
> 
> end

Am I right in guessing that you’re trying to put a copy of  
modelSource into outputModel? If so, you might want to take a look  
at this:

> **[WebMD - Better information. Better health.](https://www.webmd.com/?Make_A_Deep_Copy_Of_An_Object)**
>
> The leading source for trustworthy and timely health and medical news and information. Providing credible health information, supportive community, and educational services by blending award-winning expertise in content, community services, expert...

Depending on the structure of the Model class, it might be better to  
give it its own copy method.

matthew smillie.

---

<div class="post-metadata">

**Author:** ![sporkyy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c89c15/32.png) [@sporkyy](https://www.ruby-forum.com/u/sporkyy)\
**Post date:** [January 30, 2006, 7:43am UTC](https://www.ruby-forum.com/t/misunderstanding-the-passing-of-references/98699/3 "2006-01-30T07:43:44Z")

</div>

> Am I right in guessing that you’re trying to put a copy of  
> modelSource into outputModel?

That’s exactly the case. In reading the link you suggest it seems I  
would have to use a seperate data dumping operation. This seems a  
little strange to me. Shouldn’t there be a more simple way to create a  
copy of an object?

---

<div class="post-metadata">

**Author:** ![sporkyy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c89c15/32.png) [@sporkyy](https://www.ruby-forum.com/u/sporkyy)\
**Post date:** [January 30, 2006, 7:50am UTC](https://www.ruby-forum.com/t/misunderstanding-the-passing-of-references/98699/4 "2006-01-30T07:50:40Z")

</div>

Just to provide some direct information for anyone who might find this  
and need the help…

The below does indeed solve the problem…

class Model  
def deep\_clone  
Marshal::load(Marshal.dump(self))  
end  
end

a = Model.new  
b = Model.deep\_clone

Thank you very much for your help.

---

<div class="post-metadata">

**Author:** ![sporkyy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c89c15/32.png) [@sporkyy](https://www.ruby-forum.com/u/sporkyy)\
**Post date:** [January 30, 2006, 8:07am UTC](https://www.ruby-forum.com/t/misunderstanding-the-passing-of-references/98699/5 "2006-01-30T08:07:17Z")

</div>

On Jan 30, 2006, at 6:43, Todd S. wrote:

> In reading the link you suggest it seems I  
> would have to use a seperate data dumping operation. This seems a  
> little strange to me. Shouldn’t there be a more simple way to  
> create a  
> copy of an object?

There is and there isn’t: Object#dup and #clone both make shallow  
copies:  
[http://ruby-doc.org/core/classes/Object.html#M001067](http://ruby-doc.org/core/classes/Object.html#M001067)

There’s no built-in method to create deep copies of any object,  
though, and the serialise/unserialise thing just hacks around that in  
the quickest way (as in, quickest to write).

If it were me, unless the Model class was extraordinarily  
complicated, I’d probably just write my own copy method.

matthew smillie.

---

<div class="post-metadata">

**Author:** ![sporkyy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c89c15/32.png) [@sporkyy](https://www.ruby-forum.com/u/sporkyy)\
**Post date:** [January 30, 2006, 2:52pm UTC](https://www.ruby-forum.com/t/misunderstanding-the-passing-of-references/98699/6 "2006-01-30T14:52:40Z")

</div>

On Jan 30, 2006, at 1:50 AM, Todd S. wrote:

> a = Model.new  
> b = Model.deep\_clone
> 
> Thank you very much for your help.
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

This isn’t perfect but:  
class Object  
def deeper\_copy  
copy = self.dup  
self.instance\_variables.each do |ivar|  
copy.instance\_variable\_set(ivar,  
self.instance\_variable\_get(ivar).dup)  
end  
copy  
end  
end

Note that this is specifically designed to work for regular style  
classes (ie ones that store everything in instance variables) and  
for Strings, Arrays and Structs its no better than (but no worse  
than) #dup. Hence “deeper\_copy” instead of “deep\_copy”

---

<div class="post-metadata">

**Author:** ![sporkyy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c89c15/32.png) [@sporkyy](https://www.ruby-forum.com/u/sporkyy)\
**Post date:** [February 1, 2006, 10:54pm UTC](https://www.ruby-forum.com/t/misunderstanding-the-passing-of-references/98699/7 "2006-02-01T22:54:37Z")

</div>

Matthew S. wrote:

> [class Object - RDoc Documentation](http://ruby-doc.org/core/classes/Object.html#M001067)
> 
> There’s no built-in method to create deep copies of any object,  
> though, and the serialise/unserialise thing just hacks around that in  
> the quickest way (as in, quickest to write).
> 
> If it were me, unless the Model class was extraordinarily  
> complicated, I’d probably just write my own copy method.

You can as well override or reimplement the #dup method.

```
robert

```
