# Question - Passing parameters by reference

**URL:** <https://www.ruby-forum.com/t/question-passing-parameters-by-reference/110361>\
**Category:** Ruby\
**Created:** [July 25, 2007, 7:20pm UTC](https://www.ruby-forum.com/t/question-passing-parameters-by-reference/110361 "2007-07-25T19:20:50Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![caof2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/97f17d/32.png) [@caof2005](https://www.ruby-forum.com/u/caof2005)\
**Post date:** [July 25, 2007, 7:20pm UTC](https://www.ruby-forum.com/t/question-passing-parameters-by-reference/110361/1 "2007-07-25T19:20:50Z")

</div>

Hello Folks

My question is the following:

How can I pass a reference to a method as an argument, so after  
finishing the execution of the method the argument gets updated with a  
new value. ( known as “pass by reference” in other languages ).

example:  
…  
def changeValue( val, cad )  
temp = 0;  
val = (val \* 10) / cad.to\_i  
temp = cad.to\_i + 3.to\_i  
cad = temp.to\_s  
end  
…  
…  
val = 35;  
cad = “7”

puts "val before calling changeValue:= " + val.to\_s  
puts “cad before calling changeValue:=” + cad

changeValue( val, cad )

puts "val before calling changeValue:= " + val.to\_s #— I pretend  
to print ‘50’  
puts “cad before calling changeValue:=” + cad #— I pretend  
to print ‘10’

Regards  
Carlos

---

<div class="post-metadata">

**Author:** ![caof2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/97f17d/32.png) [@caof2005](https://www.ruby-forum.com/u/caof2005)\
**Post date:** [July 25, 2007, 7:55pm UTC](https://www.ruby-forum.com/t/question-passing-parameters-by-reference/110361/2 "2007-07-25T19:55:26Z")

</div>

On 7/25/07, caof2005 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> def changeValue( val, cad )  
> puts "val before calling changeValue:= " + val.to\_s  
> Carlos  
> Assignment inside method scope is allowed, but creates a different  
> object. What you get back from a method is what the method returns.  
> So you could mimic your desired behavior with…

def f(a, b)  
return a\*10.0/b.to\_i, (b.to\_i+3).to\_s  
end

x, y = 35, “7”  
x, y = f( x, y ) #this will make x equal to 50 and y equal to the string  
“10”

Does that make sense?

Todd

---

<div class="post-metadata">

**Author:** ![caof2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/97f17d/32.png) [@caof2005](https://www.ruby-forum.com/u/caof2005)\
**Post date:** [July 25, 2007, 8:00pm UTC](https://www.ruby-forum.com/t/question-passing-parameters-by-reference/110361/3 "2007-07-25T20:00:54Z")

</div>

On 7/25/07, Todd B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > example:  
> > cad = “7”
> 
> x, y = 35, “7”  
> x, y = f( x, y ) #this will make x equal to 50 and y equal to the string  
> “10”
> 
> Does that make sense?
> 
> Todd
> 
> What if the method is returning something else already and you don’t want  
> to have to return the changed value? How do you pass in a variable to a  
> method and have the change stick when it exists the method in ruby?  
> Chris

---

<div class="post-metadata">

**Author:** ![caof2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/97f17d/32.png) [@caof2005](https://www.ruby-forum.com/u/caof2005)\
**Post date:** [July 25, 2007, 8:36pm UTC](https://www.ruby-forum.com/t/question-passing-parameters-by-reference/110361/4 "2007-07-25T20:36:31Z")

</div>

Hi –

On Thu, 26 Jul 2007, Chris T. wrote:

> What if the method is returning something else already and you don’t want  
> to have to return the changed value? How do you pass in a variable to a  
> method and have the change stick when it exists the method in ruby?

You can change an object:

def change\_me(str)  
str.upcase!  
end

s = “abc”  
change\_me(str)  
puts s # ABC

But you can’t change the bindings. In the example, I’ve got s and str  
both containing references to the same string. But the names of the  
identifiers themselves, s and str, are solely the business of their  
local scopes, which are different as between the method body and the  
calling context.

David

---

<div class="post-metadata">

**Author:** ![caof2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/97f17d/32.png) [@caof2005](https://www.ruby-forum.com/u/caof2005)\
**Post date:** [July 25, 2007, 8:53pm UTC](https://www.ruby-forum.com/t/question-passing-parameters-by-reference/110361/5 "2007-07-25T20:53:18Z")

</div>

On 7/25/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> You can change an object:  
> both containing references to the same string. But the names of the  
> RUBY FOR RAILS ([Ruby for Rails](http://www.manning.com/black))
> 
> - Ruby/Rails training  
> & consulting: Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))
> 
> I’m talking in a case like this:  
> def add\_one(number)  
> number+=1  
> end  
> n = 1  
> add\_one(n)

While the method returns 2, the value of n when the method is done is  
still  
one.

Or with strings  
def add\_bar(st)  
st + “bar”  
end

s = “foo”  
add\_bar(s)

Same thing. The method returns foobar, but the value of s is unchaged.  
Is there a way to change the values of these variables from within the  
method and have the values remain changed when the method has exited?

Chris

---

<div class="post-metadata">

**Author:** ![caof2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/97f17d/32.png) [@caof2005](https://www.ruby-forum.com/u/caof2005)\
**Post date:** [July 25, 2007, 8:57pm UTC](https://www.ruby-forum.com/t/question-passing-parameters-by-reference/110361/6 "2007-07-25T20:57:44Z")

</div>

I believe it’s adding & in front of the argument in a method in C++ to  
get  
pass by reference.  
void add\_one(&num) {  
num++  
}

Is there something that’s equivalent in ruby?

Chris

---

<div class="post-metadata">

**Author:** ![caof2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/97f17d/32.png) [@caof2005](https://www.ruby-forum.com/u/caof2005)\
**Post date:** [July 25, 2007, 9:01pm UTC](https://www.ruby-forum.com/t/question-passing-parameters-by-reference/110361/7 "2007-07-25T21:01:08Z")

</div>

On Jul 25, 10:54 am, “Todd B.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > new value. ( known as “pass by reference” in other languages ).  
> > …  
> > puts “cad before calling changeValue:=” + cad #— I pretend  
> > return a\*10.0/b.to\_i, (b.to\_i+3).to\_s  
> > end
> 
> x, y = 35, “7”  
> x, y = f( x, y ) #this will make x equal to 50 and y equal to the string “10”
> 
> Does that make sense?
> 
> Todd

Todd:

Although it did not change neither of both parameters and I suppose  
you can’t do this in Ruby, at the end you showed me a new technique  
that can mimic the behavior.

Thanks so much for this tip!!!

Carlos

---

<div class="post-metadata">

**Author:** ![caof2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/97f17d/32.png) [@caof2005](https://www.ruby-forum.com/u/caof2005)\
**Post date:** [July 25, 2007, 9:02pm UTC](https://www.ruby-forum.com/t/question-passing-parameters-by-reference/110361/8 "2007-07-25T21:02:21Z")

</div>

On 7/25/07, Chris T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> a
> 
> > ## puts s # ABC
> 
> n = 1
> 
> s = “foo”  
> add\_bar(s)
> 
> Same thing. The method returns foobar, but the value of s is unchaged.  
> Is there a way to change the values of these variables from within the  
> method and have the values remain changed when the method has exited?
> 
> Chris

n = add\_one(n) would fix your given case. I assume it is more  
complicated  
than this?

–  
“Hey brother christian with your high and mighty errand, Your actions  
speak  
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)

---

<div class="post-metadata">

**Author:** ![caof2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/97f17d/32.png) [@caof2005](https://www.ruby-forum.com/u/caof2005)\
**Post date:** [July 25, 2007, 8:59pm UTC](https://www.ruby-forum.com/t/question-passing-parameters-by-reference/110361/9 "2007-07-25T20:59:58Z")

</div>

> end
> 
> s = “abc”

did you mean to say…

```
s = str = "abc"

```

?

---

<div class="post-metadata">

**Author:** ![caof2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/97f17d/32.png) [@caof2005](https://www.ruby-forum.com/u/caof2005)\
**Post date:** [July 25, 2007, 9:07pm UTC](https://www.ruby-forum.com/t/question-passing-parameters-by-reference/110361/10 "2007-07-25T21:07:51Z")

</div>

Chris T. wrote the following on 25.07.2007 20:56 :

> I believe it’s adding & in front of the argument in a method in C++ to  
> get  
> pass by reference.  
> void add\_one(&num) {  
> num++  
> }
> 
> Is there something that’s equivalent in ruby?

Ruby always pass references. The problem is that “number += 1” is  
expanded as “number = number + 1”.

The problem here is that Fixnum#+ doesn’t modify the object but  
generates another one. So in fact it generates what happens is that a  
new Object is created and then is referenced by number, but not by n. To  
make it work like you want you would need to only use methods that  
modify the object itself, which AFAIR don’t exist in this context.

Lionel.

---

<div class="post-metadata">

**Author:** ![caof2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/97f17d/32.png) [@caof2005](https://www.ruby-forum.com/u/caof2005)\
**Post date:** [July 25, 2007, 9:16pm UTC](https://www.ruby-forum.com/t/question-passing-parameters-by-reference/110361/11 "2007-07-25T21:16:49Z")

</div>

Thanks for the response, although I was just wondering that using  
Strings as a parameters in a method definition is a special case.  
Let me explain…  
I observed that when you use a string as a parameter in a method  
definition, it can be changed by applying several operations inside  
the method, for instance you can change it, by adding letters to the  
string, changing the content to uppercase etc.  
So I figured out that when you use a string, what really happens  
inside the method is that you don’t create a new object, you just use  
the reference.  
In opposition as if you use other kind of parameters in a method  
definition, let say for example an int variable.  
If you try to change this int variable inside the method, it  
simply doesn’t work, what happens is that you get a “copy” of the  
object that you’re passing but after the method is finished, that  
copy disappears and at the end the parameter that you passed was not  
affected.  
Am I correct with my conclusions?

Regards  
Carlos

---

<div class="post-metadata">

**Author:** ![caof2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/97f17d/32.png) [@caof2005](https://www.ruby-forum.com/u/caof2005)\
**Post date:** [July 25, 2007, 9:33pm UTC](https://www.ruby-forum.com/t/question-passing-parameters-by-reference/110361/12 "2007-07-25T21:33:41Z")

</div>

On 7/25/07, Lionel B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Yup, that’s what I was wondering.  
> Sorry to derail the thread a bit there.

Chris

---

<div class="post-metadata">

**Author:** ![caof2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/97f17d/32.png) [@caof2005](https://www.ruby-forum.com/u/caof2005)\
**Post date:** [July 25, 2007, 9:34pm UTC](https://www.ruby-forum.com/t/question-passing-parameters-by-reference/110361/13 "2007-07-25T21:34:47Z")

</div>

On 7/25/07, caof2005 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> In opposition as if you use other kind of parameters in a method
> 
> ```
> 
> definition, let say for example an int variable.  
> If you try to change this int variable inside the method, it  
> simply doesn’t work, what happens is that you get a “copy” of the  
> object that you’re passing but after the method is finished, that  
> copy disappears and at the end the parameter that you passed was not  
> affected.  
> Am I correct with my conclusions?
> 
> Regards  
> Carlos

Yes, because a Fixnum object is immutable. Also, like David said, you  
cannot change bindings. See…

irb\> def f x; x = 6; end  
=\> nil  
irb\> a = 1  
=\> 1  
irb\> f a  
=\> 6  
irb\> a  
=\> 1

Now look at something that is mutable, like String…  
irb\> a = “hello”  
=\> “hello”  
irb\> def g x; x \<\< “bye”; end  
=\> nil  
irb\> f a  
“hellobye”  
irb\> a  
“hellobye”

But watch this assignment…  
irb\> a = “hello”  
=\> “hello”  
irb\> a. **id**  
=\> 68099810  
irb\> def g x  
irb\> puts x. **id**  
irb\> x = “bye” #here is an attempt at assignment  
irb\> puts x. **id** #oops, not correct scope, created new string  
object with name x  
irb\> end  
=\> nil  
irb\> g a  
68099810  
68052050  
=\> nil  
irb\> a #should be unchanged  
=\> “hello”

So yes, mutable objects you can change within method scope, but not  
reassign. To answer Chris’s question about what you should do when  
you are already returning something, well, you can return more than  
one thing. You can also send an object as a parameter (Array, Hash,  
whatever), which of course can be modified within the method.

cheers,  
Todd

---

<div class="post-metadata">

**Author:** ![caof2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/97f17d/32.png) [@caof2005](https://www.ruby-forum.com/u/caof2005)\
**Post date:** [July 25, 2007, 10:11pm UTC](https://www.ruby-forum.com/t/question-passing-parameters-by-reference/110361/14 "2007-07-25T22:11:04Z")

</div>

Hi –

On Thu, 26 Jul 2007, Philip H. wrote:

> > end
> > 
> > s = “abc”
> 
> did you mean to say…
> 
> s = str = “abc”
> 
> ?
> 
> > change\_me(str)

No; I meant to say change\_me(s) 🙂

David

---

<div class="post-metadata">

**Author:** ![caof2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/97f17d/32.png) [@caof2005](https://www.ruby-forum.com/u/caof2005)\
**Post date:** [July 25, 2007, 10:29pm UTC](https://www.ruby-forum.com/t/question-passing-parameters-by-reference/110361/15 "2007-07-25T22:29:15Z")

</div>

I think it’s the clearest explanation so far Jason.

I believe it’s a good practice to stop thinking in terms of pointers  
and start thinking in terms of objects.  
You don’t pass a variable by reference, you pass an object.

With a little time you’ll find this approach a lot more comfortable.

Cheers !

---

<div class="post-metadata">

**Author:** ![caof2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/97f17d/32.png) [@caof2005](https://www.ruby-forum.com/u/caof2005)\
**Post date:** [July 25, 2007, 9:35pm UTC](https://www.ruby-forum.com/t/question-passing-parameters-by-reference/110361/16 "2007-07-25T21:35:57Z")

</div>

You’re pretty close, just some details as to the whyness:

String are of course instances of the String class. This is simply an  
array  
of characters, of which you can modify freely, as you’ve discovered.

Numbers, on the other hand are special case instances of FixNum,  
BigNumber,  
any subclass of Numeric. These have no underlying data representation,  
they  
_are_ the number they represent. So if you tried to do the same to  
Fixnums  
like you can with String, you’d actually be trying to change 1 to equal  
2,  
and you can imagine what kind of havok that would cause.

So with Strings, you have an underlying character array you can play  
with,  
but with Numerics you have to change the number your variable is  
pointing  
to.

Hopefully I made that understandable, though please correct me if  
something  
I said was wrong.

Jason

---

<div class="post-metadata">

**Author:** ![caof2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/97f17d/32.png) [@caof2005](https://www.ruby-forum.com/u/caof2005)\
**Post date:** [August 6, 2007, 9:16pm UTC](https://www.ruby-forum.com/t/question-passing-parameters-by-reference/110361/17 "2007-08-06T21:16:14Z")

</div>

On Jul 25, 2007, at 4:43 PM, removed\_email\_address@domain.invalid wrote:

> 

I go even farther and simply think of symbols and integers to also be  
references. Within that context, the literal 1 is simply a textual  
representation of a reference to the object that behaves like the  
integer 1.  
The fact that the Ruby interpreter doesn’t actually allocate memory for  
the object itself is just an implementation detail.

Gary W.

---

<div class="post-metadata">

**Author:** ![caof2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/97f17d/32.png) [@caof2005](https://www.ruby-forum.com/u/caof2005)\
**Post date:** [July 25, 2007, 10:43pm UTC](https://www.ruby-forum.com/t/question-passing-parameters-by-reference/110361/18 "2007-07-25T22:43:57Z")

</div>

Hi –

On Thu, 26 Jul 2007, Okushi wrote:

> I think it’s the clearest explanation so far Jason.
> 
> I believe it’s a good practice to stop thinking in terms of pointers  
> and start thinking in terms of objects.  
> You don’t pass a variable by reference, you pass an object.

Or… you pass a reference by variable 🙂 I think it’s useful to  
know that you’re passing references around:

a = “abc”  
b = a  
b \<\< “def”  
a # “abcdef”

vs. immediate values (symbols, integers), though of course it does all  
indeed converge on objects.

David

---

<div class="post-metadata">

**Author:** ![caof2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/97f17d/32.png) [@caof2005](https://www.ruby-forum.com/u/caof2005)\
**Post date:** [August 6, 2007, 9:29pm UTC](https://www.ruby-forum.com/t/question-passing-parameters-by-reference/110361/19 "2007-08-06T21:29:15Z")

</div>

Hi –

On Tue, 7 Aug 2007, Gary W. wrote:

> > indeed converge on objects.
> 
> I go even farther and simply think of symbols and integers to also be  
> references. Within that context, the literal 1 is simply a textual  
> representation of a reference to the object that behaves like the integer 1.  
> The fact that the Ruby interpreter doesn’t actually allocate memory for  
> the object itself is just an implementation detail.

I’d agree about the literal 1 in comparison with other literals:

1 # textual representation of that object  
“hi” # textual representation of that object  
[1,2,3] # etc.

but if you do this:

a = 1  
b = “hi”

now a (as I understand it) is actually bound to the integer 1, while b  
is bound to a reference to the object. (I can’t say “A reference to  
‘hi’” since that ‘hi’ would be a different one… 🙂

The reason I think it can be good to keep that difference in view is  
that it has some explanatory power – especially when the question  
arises about why there’s no a++ (which, because a _is_ 1, would be  
like 1++).

David

---

<div class="post-metadata">

**Author:** ![caof2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/97f17d/32.png) [@caof2005](https://www.ruby-forum.com/u/caof2005)\
**Post date:** [August 7, 2007, 2:16am UTC](https://www.ruby-forum.com/t/question-passing-parameters-by-reference/110361/20 "2007-08-07T02:16:42Z")

</div>

On 8/6/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > b = a  
> > > integer 1.
> 
> like 1++).  
> If 1 and “hi” were methods, they might look like this:

val = 1  
define\_method(:one) { val }

define\_method(:hi) { val = String.new; val[0] = ?h; val[1] = ?i; val }

a = one()  
b = hi()  
c = hi()  
d = one()

this seemed to illustrate it better in my head. Seeing it typed out, it  
doesn’t look so insightful. I guess what I’m trying to say is. don’t  
think  
of literals as ways of referring to objects, think of them as method  
calls  
that return objects. Or something like that.

David

[Next page](https://www.ruby-forum.com/t/question-passing-parameters-by-reference/110361.md?page=2)
