# Does Ruby have reference parameter?

**URL:** <https://www.ruby-forum.com/t/does-ruby-have-reference-parameter/193258>\
**Category:** Ruby\
**Created:** [August 14, 2010, 9:17am UTC](https://www.ruby-forum.com/t/does-ruby-have-reference-parameter/193258 "2010-08-14T09:17:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![cnguyen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@cnguyen](https://www.ruby-forum.com/u/cnguyen)\
**Post date:** [August 14, 2010, 9:17am UTC](https://www.ruby-forum.com/t/does-ruby-have-reference-parameter/193258/1 "2010-08-14T09:17:18Z")

</div>

Hi everyone,  
In C++, in order to modify a variable being passed into a function we do

```auto
void func( int& x )
{
    x = 10;
}
```

I wonder is there a way to do something like this in Ruby or a similar  
idea? If there’s no reference type then how would we be able to modify a  
varible?

Thanks,

---

<div class="post-metadata">

**Author:** ![cnguyen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@cnguyen](https://www.ruby-forum.com/u/cnguyen)\
**Post date:** [August 14, 2010, 10:39am UTC](https://www.ruby-forum.com/t/does-ruby-have-reference-parameter/193258/2 "2010-08-14T10:39:12Z")

</div>

On Sat, Aug 14, 2010 at 9:17 AM, Chan N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> idea? If there’s no reference type then how would we be able to modify a  
> varible?

In Ruby a variable is a reference to an object, and when you call a  
method passing the variable as a parameter, what the method gets it’s  
a copy of the reference, so you can’t modify a variable inside a  
method. That means, changing which object the variable refers to:

def change(arg)  
arg = “another thing”  
end

a = “a string”  
change(a)

a would still be referencing “a string”, because arg inside the method  
is a copy of the reference. The usual way is to have the method return  
the new value and assign it:

def change(arg)  
“a better #{arg}”  
end

a = “a string”  
a = change(a)

or to use mutable methods:

def change(arg)  
arg.replace(“a better #{arg}”)  
end

a = “a string”  
change(a)

This works because you are not changing which object is referenced by  
a, but you change the internal state of that object.

Jesus.

---

<div class="post-metadata">

**Author:** ![cnguyen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@cnguyen](https://www.ruby-forum.com/u/cnguyen)\
**Post date:** [August 14, 2010, 1:09pm UTC](https://www.ruby-forum.com/t/does-ruby-have-reference-parameter/193258/3 "2010-08-14T13:09:38Z")

</div>

On Sat, Aug 14, 2010 at 3:17 AM, Chan N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> idea? If there’s no reference type then how would we be able to modify a  
> varible?

[http://talklikeaduck.denhaven2.com/2006/09/13/on-variables-values-and-objects](http://talklikeaduck.denhaven2.com/2006/09/13/on-variables-values-and-objects)

–  
Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Github: [rubyredrick (Rick DeNatale) · GitHub](http://github.com/rubyredrick)  
Twitter: @RickDeNatale  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)

---

<div class="post-metadata">

**Author:** ![cnguyen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@cnguyen](https://www.ruby-forum.com/u/cnguyen)\
**Post date:** [August 15, 2010, 8:24am UTC](https://www.ruby-forum.com/t/does-ruby-have-reference-parameter/193258/4 "2010-08-15T08:24:09Z")

</div>

Thanks a lot everybody,  
I’m just too new to Ruby, I will read the book and practice more. The  
last code snippet is really interesting. Thanks Caleb C., I will  
never forget it 😉

---

<div class="post-metadata">

**Author:** ![cnguyen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@cnguyen](https://www.ruby-forum.com/u/cnguyen)\
**Post date:** [August 14, 2010, 7:00pm UTC](https://www.ruby-forum.com/t/does-ruby-have-reference-parameter/193258/5 "2010-08-14T19:00:15Z")

</div>

On 8/14/10, Chan N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> idea? If there’s no reference type then how would we be able to modify a  
> varible?

On the (rare) occasions when I have needed this, I make the parameter  
an array containing a single value instead. Your c++ code could  
translate to ruby as this:

def func(x\_array)  
x\_array[0]=10  
end

#call it like this:  
var=[9]  
func(var)  
p var[0] #=\>10

This is basically what c++ does with reference parameters under the  
surface anyway. This is more awkward in ruby, since you have to  
explicitly dereference your array in the method and afterwards to get  
the changed value. The awkwardness is only a minor annoyance and  
should serve as a reminder to you to find another way to do it. The  
other answers in this thread offer some good advice.

---

<div class="post-metadata">

**Author:** ![cnguyen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@cnguyen](https://www.ruby-forum.com/u/cnguyen)\
**Post date:** [August 16, 2010, 10:47pm UTC](https://www.ruby-forum.com/t/does-ruby-have-reference-parameter/193258/6 "2010-08-16T22:47:11Z")

</div>

On 8/15/2010 12:24 AM, Chan N. wrote:

> Thanks a lot everybody,  
> I’m just too new to Ruby, I will read the book and practice more. The  
> last code snippet is really interesting. Thanks Caleb C., I will  
> never forget it 😉  
> Just to add… the only instance that I have found where I “need” to do  
> this in C++ is if I have multiple values I want to change, otherwise I  
> can just return the new value. Since you can return multiple values  
> from a Ruby function, this is a non-issue:  
> irb(main):001:0\> def foo  
> irb(main):002:1\> return :bar, :foo  
> irb(main):003:1\> end  
> =\> nil  
> irb(main):004:0\> a,b=foo  
> =\> [:bar, :foo]  
> irb(main):005:0\> a  
> =\> :bar  
> irb(main):006:0\> b  
> =\> :foo  
> irb(main):007:0\>
