# Assignment as a reference?

**URL:** <https://rubytalk.org/t/assignment-as-a-reference/40060>\
**Category:** ruby-talk\
**Created:** [20 August 2007 16:20 UTC](https://rubytalk.org/t/assignment-as-a-reference/40060 "2007-08-20T16:20:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Morgan\_Grubb](https://avatars.discourse-cdn.com/v4/letter/m/51bf81/32.png) [@Morgan\_Grubb](https://rubytalk.org/u/Morgan_Grubb)\
**Post date:** [20 August 2007 16:20 UTC](https://rubytalk.org/t/assignment-as-a-reference/40060/1 "2007-08-20T16:20:42Z")

</div>

Hi,

I ran into a simple issue earlier with assignment seeming to make  
references. I'm new to Ruby so perhaps this is how it's supposed to be. Here  
is an abridged version of what happened:

foo = 'bar' #=\> bar  
foo2 = foo #=\> bar  
foo = foo.gsub 'bar', 'wibble' #=\> wibble  
foo2 #=\> bar

foo = 'bar' #=\> bar  
foo2 = foo #=\> bar  
foo.gsub! 'bar', 'wibble' #=\> wibble  
foo2 #=\> wibble

You can paste those straight into irb and get the same output. Could someone  
please explain why the second example changes foo2?

Cheers,  
Morgan Grubb.

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [20 August 2007 16:24 UTC](https://rubytalk.org/t/assignment-as-a-reference/40060/2 "2007-08-20T16:24:14Z")

</div>

Hi --

> Hi,
> 
> I ran into a simple issue earlier with assignment seeming to make  
> references. I'm new to Ruby so perhaps this is how it's supposed to be. Here

It is.

> is an abridged version of what happened:
> 
> foo = 'bar' #=\> bar  
> foo2 = foo #=\> bar  
> foo = foo.gsub 'bar', 'wibble' #=\> wibble  
> foo2 #=\> bar
> 
> foo = 'bar' #=\> bar  
> foo2 = foo #=\> bar  
> foo.gsub! 'bar', 'wibble' #=\> wibble  
> foo2 #=\> wibble
> 
> You can paste those straight into irb and get the same output. Could someone  
> please explain why the second example changes foo2?

Ruby assignments pretty much traffic in references. When you do:

&nbsp;&nbsp;&nbsp;foo2 = foo

you're giving foo2 a copy of the reference that foo contains. When you  
change the object, you see the change reflected through any reference  
to it.

David

> **···**
>
> On Tue, 21 Aug 2007, Morgan Grubb wrote:
> 
> --  
> \* Books:  
> &nbsp;&nbsp;&nbsp;RAILS ROUTING (new! [http://www.awprofessional.com/title/0321509242\](http://www.awprofessional.com/title/0321509242%5C))  
> &nbsp;&nbsp;&nbsp;RUBY FOR RAILS ([http://www.manning.com/black\](http://www.manning.com/black%5C))  
> \* Ruby/Rails training  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;& consulting: Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![Douglas\_F\_Shearer](https://avatars.discourse-cdn.com/v4/letter/d/59ef9b/32.png) [@Douglas\_F\_Shearer](https://rubytalk.org/u/Douglas_F_Shearer)\
**Post date:** [20 August 2007 16:28 UTC](https://rubytalk.org/t/assignment-as-a-reference/40060/3 "2007-08-20T16:28:22Z")

</div>

> foo = 'bar' #=\> bar  
> foo2 = foo #=\> bar  
> foo.gsub! 'bar', 'wibble' #=\> wibble  
> foo2 #=\> wibble

What you have experienced is reference assignment. If you wish to duplicate only the content, and not the reference, then you should to use .dup.

\> foo = "bar"  
=\> "bar"  
\> foo2 = foo.dup  
=\> "bar"  
\> foo = "wibble"  
=\> "wibble"  
\> foo2  
=\> "bar"

Hope this helps you out.

Douglas F Shearer  
dougal.s@gmail.com

> **[Douglas F Shearer](https://douglasfshearer.com/)**
>
> Software developer, mountainbiker.

> **···**
>
> On 20 Aug 2007, at 17:20, Morgan Grubb wrote:

---

<div class="post-metadata">

**Author:** ![Morgan\_Grubb](https://avatars.discourse-cdn.com/v4/letter/m/51bf81/32.png) [@Morgan\_Grubb](https://rubytalk.org/u/Morgan_Grubb)\
**Post date:** [20 August 2007 16:34 UTC](https://rubytalk.org/t/assignment-as-a-reference/40060/4 "2007-08-20T16:34:05Z")

</div>

Thanks for that.

I've been reading through quite a few references and manuals (working  
through Agile Web Development at the moment) and the fact that every  
assignment is a reference hasn't really been mentioned that much. Guess I've  
missed it or haven't gotten that far.

Cheers.

> **···**
>
> On 20/08/07, Douglas F Shearer \<dougal.s@gmail.com\> wrote:
> 
> > On 20 Aug 2007, at 17:20, Morgan Grubb wrote:  
> > \> foo = 'bar' #=\> bar  
> > \> foo2 = foo #=\> bar  
> > \> foo.gsub! 'bar', 'wibble' #=\> wibble  
> > \> foo2 #=\> wibble
> > 
> > What you have experienced is reference assignment. If you wish to  
> > duplicate only the content, and not the reference, then you should to  
> > use .dup.
> > 
> > \> foo = "bar"  
> > =\> "bar"  
> > \> foo2 = foo.dup  
> > =\> "bar"  
> > \> foo = "wibble"  
> > =\> "wibble"  
> > \> foo2  
> > =\> "bar"
> > 
> > Hope this helps you out.
> > 
> > Douglas F Shearer  
> > dougal.s@gmail.com  
> > [http://douglasfshearer.com](http://douglasfshearer.com)
