# Pass by reference?

**URL:** <https://www.ruby-forum.com/t/pass-by-reference/83534>\
**Category:** Ruby\
**Created:** [January 27, 2007, 12:28am UTC](https://www.ruby-forum.com/t/pass-by-reference/83534 "2007-01-27T00:28:29Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![ddancer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ecb155/32.png) [@ddancer](https://www.ruby-forum.com/u/ddancer)\
**Post date:** [January 27, 2007, 12:28am UTC](https://www.ruby-forum.com/t/pass-by-reference/83534/1 "2007-01-27T00:28:29Z")

</div>

Hello,

Is there a way to pass variables by reference into function.

I have large string to pass and the pass by value seems to be eating up  
too much memory.

Thanks,

Andy

---

<div class="post-metadata">

**Author:** ![ddancer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ecb155/32.png) [@ddancer](https://www.ruby-forum.com/u/ddancer)\
**Post date:** [January 27, 2007, 12:33am UTC](https://www.ruby-forum.com/t/pass-by-reference/83534/2 "2007-01-27T00:33:29Z")

</div>

Andy K. wrote:

> Hello,
> 
> Is there a way to pass variables by reference into function.
> 
> I have large string to pass and the pass by value seems to be eating up  
> too much memory.

You don’t actually need to do anything: everything in ruby is passed  
by reference, (excepted integers, symbols, nil, false and true - am I  
missing one ?)

Cheers,

Vincent

---

<div class="post-metadata">

**Author:** ![ddancer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ecb155/32.png) [@ddancer](https://www.ruby-forum.com/u/ddancer)\
**Post date:** [January 27, 2007, 12:35am UTC](https://www.ruby-forum.com/t/pass-by-reference/83534/3 "2007-01-27T00:35:05Z")

</div>

Everything is pass-by-reference in Ruby. There is no pass-by-value. So  
your memory problems must have another cause.

Andy K. wrote:

> Hello,
> 
> Is there a way to pass variables by reference into function.
> 
> I have large string to pass and the pass by value seems to be eating up  
> too much memory.
> 
> Thanks,
> 
> Andy

---

<div class="post-metadata">

**Author:** ![ddancer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ecb155/32.png) [@ddancer](https://www.ruby-forum.com/u/ddancer)\
**Post date:** [January 27, 2007, 1:16am UTC](https://www.ruby-forum.com/t/pass-by-reference/83534/4 "2007-01-27T01:16:33Z")

</div>

Hi –

On Sat, 27 Jan 2007, Per V. wrote:

> Andy K. wrote:
> 
> > Hello,
> > 
> > Is there a way to pass variables by reference into function.
> > 
> > I have large string to pass and the pass by value seems to be eating up  
> > too much memory.
> 
> Everything is pass-by-reference in Ruby. There is no pass-by-value. So  
> your memory problems must have another cause.

I’ve always looked at it as: pass by value, where the “values” are  
(mostly) references. For example:

a = “abcde” # a now contains a reference to that string  
do\_something(a) # pass the reference contained in a

David

---

<div class="post-metadata">

**Author:** ![ddancer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ecb155/32.png) [@ddancer](https://www.ruby-forum.com/u/ddancer)\
**Post date:** [January 27, 2007, 1:25am UTC](https://www.ruby-forum.com/t/pass-by-reference/83534/5 "2007-01-27T01:25:33Z")

</div>

Phrogz wrote:

> If you pass an immutable type by reference, does it make a sound?  
> Er, I mean…  
> If you passed an immutable type by reference, how would you know that  
> it wasn’t passed by value?

Is there any way for the function you’re calling to modify the value of  
the variable in the caller? Pass by reference can do that.

- Martin

---

<div class="post-metadata">

**Author:** ![ddancer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ecb155/32.png) [@ddancer](https://www.ruby-forum.com/u/ddancer)\
**Post date:** [January 27, 2007, 12:55am UTC](https://www.ruby-forum.com/t/pass-by-reference/83534/6 "2007-01-27T00:55:31Z")

</div>

Per V. schrieb:

> Everything is pass-by-reference in Ruby. There is no pass-by-value. So  
> your memory problems must have another cause.

May be, that you do in fact a copy by some other operations, that create  
a new  
String object and copy the original. E.g.

s = t + ‘’

creates a new string with the same contents. There are several  
possibilities to  
do things like that without recognizing it.

Wolfgang NÃ¡dasi-Donner

---

<div class="post-metadata">

**Author:** ![ddancer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ecb155/32.png) [@ddancer](https://www.ruby-forum.com/u/ddancer)\
**Post date:** [January 27, 2007, 1:20am UTC](https://www.ruby-forum.com/t/pass-by-reference/83534/7 "2007-01-27T01:20:33Z")

</div>

On Jan 26, 4:32 pm, Vincent F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> You don’t actually need to do anything: everything in ruby is passed  
> by reference, (excepted integers, symbols, nil, false and true - am I  
> missing one ?)

How do you know if those are passed by reference or by value? It’s a  
very existential question:  
If you pass an immutable type by reference, does it make a sound?  
Er, I mean…  
If you passed an immutable type by reference, how would you know that  
it wasn’t passed by value?

I leave you with this:

irb(main):001:0\> values = [1, :foo, true, false, nil]  
=\> [1, :foo, true, false, nil]

irb(main):002:0\> puts values.map{ |v| v.object\_id }  
3  
231138  
2  
0  
4  
=\> nil

irb(main):003:0\> def foo( \*args ); puts args.map{ |v| v.object\_id };  
end  
=\> nil

irb(main):004:0\> foo( \*values )  
3  
231138  
2  
0  
4  
=\> nil

irb(main):005:0\> b = :foo  
=\> :foo

irb(main):006:0\> b.object\_id  
=\> 231138

It’s almost the reverse of what you said - it’s not that immediate  
values are passed by value, but rather that every reference to an  
immediate value refers to the same object.

---

<div class="post-metadata">

**Author:** ![ddancer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ecb155/32.png) [@ddancer](https://www.ruby-forum.com/u/ddancer)\
**Post date:** [January 27, 2007, 1:44am UTC](https://www.ruby-forum.com/t/pass-by-reference/83534/8 "2007-01-27T01:44:35Z")

</div>

Hi –

On Sat, 27 Jan 2007, Martin C. Martin wrote:

> Phrogz wrote:
> 
> > If you pass an immutable type by reference, does it make a sound?  
> > Er, I mean…  
> > If you passed an immutable type by reference, how would you know that  
> > it wasn’t passed by value?
> 
> Is there any way for the function you’re calling to modify the value of the  
> variable in the caller? Pass by reference can do that.

You can modify the object to which the variable refers:

def change\_me(obj)  
obj \<\< “hi”  
end

arr = [1,2,3]  
change\_me(arr)  
p arr # [1, 2, 3, “hi”]

In this example, arr contains a reference to an array. In change\_me,  
obj contains another copy of the same reference, so you can use it to  
manipulate and change the original array.

I still wouldn’t call this pass by reference (see my earlier post in  
this thread).

David

---

<div class="post-metadata">

**Author:** ![ddancer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ecb155/32.png) [@ddancer](https://www.ruby-forum.com/u/ddancer)\
**Post date:** [January 27, 2007, 8:52am UTC](https://www.ruby-forum.com/t/pass-by-reference/83534/9 "2007-01-27T08:52:02Z")

</div>

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

> > > If you passed an immutable type by reference, how would you know that  
> > > end  
> > > this thread).  
> > > Just to demonstrate your point

def modifying a\_param  
a\_param = “hi”  
end

a\_value = “low”

modifying a\_value  
case a\_value  
when “hi”  
puts “Passed by reference”  
when “low”  
puts “Passed by value”  
else  
puts “Passed away”  
end

or to put it different  
x = a.object\_id  
method a  
a.object\_id == x # for sure

Robert

David

---

<div class="post-metadata">

**Author:** ![ddancer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ecb155/32.png) [@ddancer](https://www.ruby-forum.com/u/ddancer)\
**Post date:** [January 27, 2007, 8:51am UTC](https://www.ruby-forum.com/t/pass-by-reference/83534/10 "2007-01-27T08:51:01Z")

</div>

On 27.01.2007 01:43, removed\_email\_address@domain.invalid wrote:

> > Is there any way for the function you’re calling to modify the value  
> > p arr # [1, 2, 3, “hi”]
> 
> In this example, arr contains a reference to an array. In change\_me,  
> obj contains another copy of the same reference, so you can use it to  
> manipulate and change the original array.
> 
> I still wouldn’t call this pass by reference (see my earlier post in  
> this thread).

Absolutely right: Ruby uses pass by value - with references.

irb(main):004:0\> def foo(x) x = 10 end  
=\> nil  
irb(main):005:0\> def bar; x = 20; foo(x); x end  
=\> nil  
irb(main):006:0\> bar  
=\> 20  
irb(main):007:0\>

There is no standard way (i.e. other than involving eval and  
metaprogramming magic) to make a variable in a calling scope point to  
another object. And, btw, this is independent of the object that the  
variable refers to. Immediate objects in Ruby seamlessly integrate with  
the rest (different like POD’s in Java for example) and from a Ruby  
language perspective you don’t see any difference (other than  
performance maybe). This is one of the reasons why Ruby is so elegant.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![ddancer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ecb155/32.png) [@ddancer](https://www.ruby-forum.com/u/ddancer)\
**Post date:** [January 27, 2007, 12:05pm UTC](https://www.ruby-forum.com/t/pass-by-reference/83534/11 "2007-01-27T12:05:23Z")

</div>

Hi –

On Sat, 27 Jan 2007, Robert D. wrote:

> > > > Er, I mean…  
> > > > obj \<\< “hi”  
> > > > I still wouldn’t call this pass by reference (see my earlier post in
> 
> modifying a\_value  
> case a\_value  
> when “hi”  
> puts “Passed by reference”  
> when “low”  
> puts “Passed by value”  
> else  
> puts “Passed away”  
> end

That’s a somewhat different point, though, having to do with variable  
scope and (re)assignment semantics. What you’re doing here  
essentially is:

a = “low”  
b = a  
b = “hi”  
puts a # low

That’s going to happen whether a method call is involved or not.

David

---

<div class="post-metadata">

**Author:** ![ddancer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ecb155/32.png) [@ddancer](https://www.ruby-forum.com/u/ddancer)\
**Post date:** [January 27, 2007, 12:11pm UTC](https://www.ruby-forum.com/t/pass-by-reference/83534/12 "2007-01-27T12:11:25Z")

</div>

Hi –

On Sat, 27 Jan 2007, Robert K. wrote:

> > > 
> > 
> > change\_me(arr)
> 
> irb(main):004:0\> def foo(x) x = 10 end  
> =\> nil  
> irb(main):005:0\> def bar; x = 20; foo(x); x end  
> =\> nil  
> irb(main):006:0\> bar  
> =\> 20  
> irb(main):007:0\>

I’m not sure that demonstrates the “values that are references” thing,  
though. You’re reassigning to x in foo, which creates a new local x;  
but I think that’s just part of the assignment semantics. Or are you  
assuming that if pass by reference were involved, then assignment  
would work differently?

David

---

<div class="post-metadata">

**Author:** ![ddancer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ecb155/32.png) [@ddancer](https://www.ruby-forum.com/u/ddancer)\
**Post date:** [January 27, 2007, 12:13pm UTC](https://www.ruby-forum.com/t/pass-by-reference/83534/13 "2007-01-27T12:13:25Z")

</div>

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

> puts a # low
> 
> That’s going to happen whether a method call is involved or not.

David, it is not my fault that parameter passing by value has the same  
semantics as reference assignment 😉 (it probably is what is happening  
behind the scenes, a new reference is allocated and the formal parameter  
is  
copied to it).  
Do you see what I wanted to show?

Cheers  
Robert

David

---

<div class="post-metadata">

**Author:** ![ddancer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ecb155/32.png) [@ddancer](https://www.ruby-forum.com/u/ddancer)\
**Post date:** [January 27, 2007, 12:35pm UTC](https://www.ruby-forum.com/t/pass-by-reference/83534/14 "2007-01-27T12:35:26Z")

</div>

> > > > > Code \>\>\>\>\>

def modifying(a\_param)  
a\_param = “hi”  
end

def modifying2(a\_param=a\_value)  
a\_param = “hi”  
end

def modifying3(a\_param)  
a\_param[0…-1]= “hi”  
end

a\_value = “low”  
modifying a\_value  
case a\_value  
when “hi”  
puts “Passed by reference”  
when “low”  
puts “Passed by value”  
else  
puts “Passed away”  
end

a\_value = “low”  
modifying2 a\_value  
case a\_value  
when “hi”  
puts “Passed by reference”  
when “low”  
puts “Passed by value”  
else  
puts “Passed away”  
end

a\_value = “low”  
modifying3 a\_value  
case a\_value  
when “hi”  
puts “Passed by reference”  
when “low”  
puts “Passed by value”  
else  
puts “Passed away”  
end

> > > > > Output \>\>\>\>\>

Passed by value  
Passed by value  
Passed by reference

> > > > > EoE \>\>\>\>\>

I would name it somehow “call by assignment of value to local variable”,  
because  
the value of the actual parameter is assigned to the local variable used  
as a  
formal parameter.

except for “Fixnum” etc. this is a pointer to an object. I one changes  
the  
object itself, it will work like “by reference”, otherwise only the  
local  
variable is affected, without having any effect to the outside world.

Wolfgang Nádasi-Donner

---

<div class="post-metadata">

**Author:** ![ddancer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ecb155/32.png) [@ddancer](https://www.ruby-forum.com/u/ddancer)\
**Post date:** [January 27, 2007, 12:47pm UTC](https://www.ruby-forum.com/t/pass-by-reference/83534/15 "2007-01-27T12:47:28Z")

</div>

Hi –

On Sat, 27 Jan 2007, Wolfgang Nádasi-Donner wrote:

> def modifying3(a\_param)  
> else  
> else  
> else  
> puts “Passed away”  
> end
> 
> > > > > > Output \>\>\>\>\>
> 
> Passed by value  
> Passed by value

But you’ve broken the bindings. It’s a whole new ballgame, as we say  
here 🙂

> Passed by reference

I would say: a reference, passed by value.

> > > > > > EoE \>\>\>\>\>
> 
> I would name it somehow “call by assignment of value to local variable”,  
> because the value of the actual parameter is assigned to the local variable  
> used as a formal parameter.
> 
> except for “Fixnum” etc. this is a pointer to an object. I one changes the  
> object itself, it will work like “by reference”, otherwise only the local  
> variable is affected, without having any effect to the outside world.

I consider the variable assignment semantics to be a separate matter.  
For example:

def x(a)  
a = “hello”  
end

Here, Ruby’s rule is that the second a is a completely new binding,  
and destroys the original binding. I don’t think that has anything to  
do with whether the first a is a reference or a value. In other  
words, even if Ruby had pass by reference, I assume that a = “hello”  
would still destroy the binding and create a new local variable.

(Of course, it’s purely speculative, since it would be a completely  
different language.)

David

---

<div class="post-metadata">

**Author:** ![ddancer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ecb155/32.png) [@ddancer](https://www.ruby-forum.com/u/ddancer)\
**Post date:** [January 27, 2007, 1:00pm UTC](https://www.ruby-forum.com/t/pass-by-reference/83534/16 "2007-01-27T13:00:30Z")

</div>

On 27.01.2007 12:10, removed\_email\_address@domain.invalid wrote:

> > > > > If you pass an immutable type by reference, does it make a sound?  
> > > > > obj \<\< “hi”  
> > > > > I still wouldn’t call this pass by reference (see my earlier post in  
> > > > > irb(main):007:0\>
> 
> I’m not sure that demonstrates the “values that are references” thing,  
> though. You’re reassigning to x in foo, which creates a new local x;  
> but I think that’s just part of the assignment semantics. Or are you  
> assuming that if pass by reference were involved, then assignment  
> would work differently?

I probably should have chosen different variable names. I was trying to  
make the point, that you cannot change a variable in bar by assigning in  
foo.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![ddancer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ecb155/32.png) [@ddancer](https://www.ruby-forum.com/u/ddancer)\
**Post date:** [January 27, 2007, 1:15pm UTC](https://www.ruby-forum.com/t/pass-by-reference/83534/17 "2007-01-27T13:15:34Z")

</div>

May be it will be clearer to name an assignment “a = someobject” by what  
it  
really means: “Give a reference to object ‘someobject’ a name ‘a’ by  
definition”. It’s close to the “:=” used for definition in mathematics.

If one uses this “a” as a “reference to ‘someobject’” as an actual  
parameter in  
a method call, it means, that the “reference to ‘someobject’” is  
actually used  
in the method.

Clearly you cannot change “a”, because you don’t have access to this  
name “a”  
inside the method, but you can change the object “someobject” usually  
without  
any problems. If you change “someobject” this will have consequences for  
‘a’,  
because the object ist changed, the reference is still the same.

Wolfgang Nádasi-Donner

---

<div class="post-metadata">

**Author:** ![ddancer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ecb155/32.png) [@ddancer](https://www.ruby-forum.com/u/ddancer)\
**Post date:** [January 27, 2007, 5:46pm UTC](https://www.ruby-forum.com/t/pass-by-reference/83534/18 "2007-01-27T17:46:10Z")

</div>

removed\_email\_address@domain.invalid schrieb:

> Right – I take that as the starting point – but what I mean is that  
> the way the assignment/rebinding semantics work doesn’t necessarily  
> pertain to the reference/value question.

I agree. reference/value came from the world before talking about  
objects. I  
remember in Algol60 somehow the wording “after ‘integer i;’ the variable  
'i IS  
an integer”.

We would never say that after writing “s = ‘A Ruby String object’” the  
variable  
‘s’ IS a String object - it gives a name for a special String object, or  
references a special String object (see 1).

Wolfgang Nádasi-Donner

(1): This wording is is used from a German, who tries to find out the  
best  
English word for what in German will be called “benennt”, or  
“bezeichnet”. I  
don’t know what wording is really good in English.

---

<div class="post-metadata">

**Author:** ![ddancer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ecb155/32.png) [@ddancer](https://www.ruby-forum.com/u/ddancer)\
**Post date:** [January 27, 2007, 12:40pm UTC](https://www.ruby-forum.com/t/pass-by-reference/83534/19 "2007-01-27T12:40:27Z")

</div>

> > > > > Code \>\>\>\>\>

def modifying(a\_param)  
a\_param = “hi”  
end

def modifying2(a\_param=a\_value)  
a\_param = “hi”  
end

def modifying3(a\_param)  
a\_param[0…-1]= “hi”  
end

a\_value = “low”  
modifying a\_value  
case a\_value  
when “hi”  
puts “Passed by reference”  
when “low”  
puts “Passed by value”  
else  
puts “Passed away”  
end

a\_value = “low”  
modifying2 a\_value  
case a\_value  
when “hi”  
puts “Passed by reference”  
when “low”  
puts “Passed by value”  
else  
puts “Passed away”  
end

a\_value = “low”  
modifying3 a\_value  
case a\_value  
when “hi”  
puts “Passed by reference”  
when “low”  
puts “Passed by value”  
else  
puts “Passed away”  
end

> > > > > Output \>\>\>\>\>

Passed by value  
Passed by value  
Passed by reference

> > > > > EoE \>\>\>\>\>

I would name it somehow “call by assignment of value to local variable”,  
because  
the value of the actual parameter is assigned to the local variable used  
as a  
formal parameter.

except for “Fixnum” etc. this is a pointer to an object. If one changes  
the  
object itself, it will work like “by reference”, otherwise only the  
local  
variable is affected, without having any effect to the outside world.

Wolfgang Nádasi-Donner

---

<div class="post-metadata">

**Author:** ![ddancer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ecb155/32.png) [@ddancer](https://www.ruby-forum.com/u/ddancer)\
**Post date:** [January 27, 2007, 1:38pm UTC](https://www.ruby-forum.com/t/pass-by-reference/83534/20 "2007-01-27T13:38:35Z")

</div>

Hi –

On Sat, 27 Jan 2007, Wolfgang Nádasi-Donner wrote:

> any problems. If you change “someobject” this will have consequences for ‘a’,  
> because the object ist changed, the reference is still the same.

Right – I take that as the starting point – but what I mean is that  
the way the assignment/rebinding semantics work doesn’t necessarily  
pertain to the reference/value question.

David

[Next page](https://www.ruby-forum.com/t/pass-by-reference/83534.md?page=2)
