Does Ruby have reference parameter?

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

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,

On Sat, Aug 14, 2010 at 9:17 AM, Chan N. [email protected]
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.

On Sat, Aug 14, 2010 at 3:17 AM, Chan N. [email protected]
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


Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Github: http://github.com/rubyredrick
Twitter: @RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale

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 :wink:

On 8/14/10, Chan N. [email protected] 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.

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 :wink:
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>

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs