Does Ruby have reference parameter?

C

Chan Nguyen

Hi everyone,
In C++, in order to modify a variable being passed into a function we do
Code:
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,
 
J

Jesús Gabriel y Galán

Hi everyone,
In C++, in order to modify a variable being passed into a function we do
Code:
void func( int& x )
{
=A0 =A0x =3D 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?

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 =3D "another thing"
end

a =3D "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 =3D "a string"
a =3D change(a)

or to use mutable methods:

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

a =3D "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.
 
R

Rick DeNatale

Hi everyone,
In C++, in order to modify a variable being passed into a function we do
Code:
void func( int& x )
{
=A0 =A0x =3D 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?

http://talklikeaduck.denhaven2.com/2006/09/13/on-variables-values-and-objec=
ts


--=20
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
 
C

Caleb Clausen

Hi everyone,
In C++, in order to modify a variable being passed into a function we do
Code:
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?

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.
 
C

Chan Nguyen

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 Clausen, I will
never forget it ;)
 
W

Walton Hoops

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 Clausen, 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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top