# Pass by reference or pass by value

**URL:** <https://www.ruby-forum.com/t/pass-by-reference-or-pass-by-value/49539>\
**Category:** Rails\
**Created:** [December 5, 2005, 9:59pm UTC](https://www.ruby-forum.com/t/pass-by-reference-or-pass-by-value/49539 "2005-12-05T21:59:22Z")\
**Posts on this page:** 15\
**Page:** 2

<div class="post-metadata">

**Author:** ![onurturgay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/ecc23a/32.png) [@onurturgay](https://www.ruby-forum.com/u/onurturgay)\
**Post date:** [December 6, 2005, 11:32pm UTC](https://www.ruby-forum.com/t/pass-by-reference-or-pass-by-value/49539/21 "2005-12-06T23:32:40Z")

</div>

Well, try to google the definition of pass-by-reference semantics.  
If a language is a object-oriented language, it doesn’t mean  
automatically  
that you can define methods with pass-by-reference arguments. Also  
immutability of some objects has nothing to do with pass-by-reference  
semantics.

For example, Java has objects, but it has strong pass-by-value  
semantics.

> **[How do employers view the UCI Cybersecurity Bootcamp? - JGuru](https://www.jguru.com/how-do-employers-view-the-uci-cybersecurity-bootcamp/)**
>
> Cybersecurity is one of the key aspects of security that companies and government agencies focus on to prevent unethical hacking and cyberattacks. Therefore,

I think we are talking about the same thing, but our definition of  
pass-by-reference is a little bit off.

Cheers,  
Kent.

---

<div class="post-metadata">

**Author:** ![onurturgay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/ecc23a/32.png) [@onurturgay](https://www.ruby-forum.com/u/onurturgay)\
**Post date:** [December 7, 2005, 12:17am UTC](https://www.ruby-forum.com/t/pass-by-reference-or-pass-by-value/49539/22 "2005-12-07T00:17:24Z")

</div>

On 7/12/2005, at 11:32 AM, Kent S. wrote:

> I think we are talking about the same thing, but our definition of  
> pass-by-reference is a little bit off.

You are quite correct, in those terms Ruby is strictly pass-by-value,  
though as you can only pass objects around it isn’t really much of a  
restriction.

–  
Phillip H.  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![onurturgay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/ecc23a/32.png) [@onurturgay](https://www.ruby-forum.com/u/onurturgay)\
**Post date:** [December 7, 2005, 2:11pm UTC](https://www.ruby-forum.com/t/pass-by-reference-or-pass-by-value/49539/23 "2005-12-07T14:11:35Z")

</div>

i think you meant to say:

m = 4  
puts change(m) \>\> 5  
puts m \>\> 4 (i want to change this to 5)

(note the change(4) is now change(m))  
assuming that i understand what you want to be doing and depending on  
the situation, one way would be to do this:

def change(n)  
n += 1 #just to have it actually do something  
end

m = 4  
m = change(m)  
puts m \>\> 5

---

<div class="post-metadata">

**Author:** ![onurturgay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/ecc23a/32.png) [@onurturgay](https://www.ruby-forum.com/u/onurturgay)\
**Post date:** [December 7, 2005, 7:21pm UTC](https://www.ruby-forum.com/t/pass-by-reference-or-pass-by-value/49539/24 "2005-12-07T19:21:57Z")

</div>

On 8/12/2005, at 1:46 AM, Vivek K. wrote:

> I am a bit confused…so how does one implement a function in ruby  
> which azctually modifies the parameter?

You don’t. I’m not sure what’s wrong with:

def change(i)  
i += 1  
end

i = 1  
i = change(i)

If you need to return multiple values just use an array.

–  
Phillip H.  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![onurturgay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/ecc23a/32.png) [@onurturgay](https://www.ruby-forum.com/u/onurturgay)\
**Post date:** [December 7, 2005, 1:47pm UTC](https://www.ruby-forum.com/t/pass-by-reference-or-pass-by-value/49539/25 "2005-12-07T13:47:10Z")

</div>

I am a bit confused…so how does one implement a function in ruby which  
azctually modifies the parameter?  
how do i make the below code change the value of m?

def change(n)  
n.ref = 5  
end

m = 4  
puts change(4) \>\> 5  
puts m \>\> 4 (i want to change this to 5)  
thanks  
vivek

---

<div class="post-metadata">

**Author:** ![onurturgay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/ecc23a/32.png) [@onurturgay](https://www.ruby-forum.com/u/onurturgay)\
**Post date:** [December 8, 2005, 12:17am UTC](https://www.ruby-forum.com/t/pass-by-reference-or-pass-by-value/49539/26 "2005-12-08T00:17:58Z")

</div>

Phillip H. wrote:

> i += 1  
> ^^^^^^ didn’t you mean: i + 1 ?  
> =====

---

<div class="post-metadata">

**Author:** ![onurturgay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/ecc23a/32.png) [@onurturgay](https://www.ruby-forum.com/u/onurturgay)\
**Post date:** [December 8, 2005, 12:22am UTC](https://www.ruby-forum.com/t/pass-by-reference-or-pass-by-value/49539/27 "2005-12-08T00:22:02Z")

</div>

On 8/12/2005, at 12:13 PM, Justin F. wrote:

> > def change(i)  
> > i += 1  
> > ^^^^^^ didn’t you mean: i + 1 ?  
> > =====  
> > end

It’ll still work, just with a redundant assignment. I was early and I  
was lacking coffee 😉

–  
Phillip H.  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![onurturgay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/ecc23a/32.png) [@onurturgay](https://www.ruby-forum.com/u/onurturgay)\
**Post date:** [December 8, 2005, 4:33am UTC](https://www.ruby-forum.com/t/pass-by-reference-or-pass-by-value/49539/28 "2005-12-08T04:33:44Z")

</div>

I just used a simplistic example but what about something like a more  
complex function?

checkStatus(statusCode) //returns an boolean true or false and sets  
statusCode

how does one implement something like this? should we use arrays?

---

<div class="post-metadata">

**Author:** ![onurturgay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/ecc23a/32.png) [@onurturgay](https://www.ruby-forum.com/u/onurturgay)\
**Post date:** [December 8, 2005, 4:41am UTC](https://www.ruby-forum.com/t/pass-by-reference-or-pass-by-value/49539/29 "2005-12-08T04:41:50Z")

</div>

On 8/12/2005, at 4:31 PM, Vivek K. wrote:

> checkStatus(statusCode) //returns an boolean true or false and  
> sets statusCode
> 
> how does one implement something like this? should we use arrays?

Yes, arrays are your friend.

def check\_status  
[true, 50]  
end

result, status\_code = check\_status

–  
Phillip H.  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![onurturgay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/ecc23a/32.png) [@onurturgay](https://www.ruby-forum.com/u/onurturgay)\
**Post date:** [December 8, 2005, 10:14pm UTC](https://www.ruby-forum.com/t/pass-by-reference-or-pass-by-value/49539/30 "2005-12-08T22:14:59Z")

</div>

What about this (to throw a wrench into things)

class Fixnum

def numcalls  
@numcalls ||= 0  
@numcalls += 1  
end

end

irb(main):010:0\> 4.numcalls  
=\> 1  
irb(main):011:0\> 4.numcalls  
=\> 2  
irb(main):012:0\> 4.numcalls  
=\> 3  
irb(main):013:0\> 4.numcalls  
=\> 4  
irb(main):014:0\> (3+1).numcalls  
=\> 5

…It would appear that all instances of the number 4 are really the  
same. What?

Matz used some pretty devious tricks to cram a lot of potential values  
into a 32 bit integer type. VALUE is…

a reference to an object OR  
a reference to a primitive (array, hash, float, omaybe more?) OR  
a 24 bit symbol table index OR  
nil OR  
true OR  
false OR  
undef OR  
a 31 bit integer

Values which don’t have an associated GC’d memory blob keep their  
“instance data” in a special hash table keyed by their object  
reference which is squeezed into the ivar routines in the interpreter  
implementation. This includes nil, true, false, symbols, and fixnums,  
as best as I can tell (it’s been a few months since I’ve dug around  
this code).

You could dynamically construct a symbol three different ways and so  
long as they == eachother, they would behave as one object instance.

That said, ruby passes by reference (i.e. changes to the objects’  
ivars persist across function call lines), but doesn’t have output  
parameters (i.e. the reference is cimmutable) or reference types  
(which would let you implement output parameters, as with pointers in  
C).

Of course you could build yourself a reference type (Ref = Struct.new  
:obj) and pass those around, but no one really does that and hopefully  
it’s not too hard to see why not. Multiple returns can be accomplished  
via hashes and arrays, which mostly removes the need for output  
parameters and is often cleaner.

Brian

On 12/8/05, Phillip H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> def check\_status
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
The years ahead pick up their dark bags.  
They move closer. There’s a slight rise in the silence

## then nothing.

(If you’re receiving this in response to mail sent to  
removed\_email\_address@domain.invalid, don’t be concerned This is my new address,  
but mail will be forwarded here indefinitely)

---

<div class="post-metadata">

**Author:** ![onurturgay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/ecc23a/32.png) [@onurturgay](https://www.ruby-forum.com/u/onurturgay)\
**Post date:** [April 8, 2008, 3:23pm UTC](https://www.ruby-forum.com/t/pass-by-reference-or-pass-by-value/49539/31 "2008-04-08T15:23:29Z")

</div>

I got it, I go it, I got it!!!

def change str  
str.replace ‘…’  
end

str = ‘.’  
change str  
puts str

#=\> …

# yey!

---

<div class="post-metadata">

**Author:** ![onurturgay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/ecc23a/32.png) [@onurturgay](https://www.ruby-forum.com/u/onurturgay)\
**Post date:** [April 8, 2008, 3:27pm UTC](https://www.ruby-forum.com/t/pass-by-reference-or-pass-by-value/49539/32 "2008-04-08T15:27:47Z")

</div>

this should be inherited from kernel/Object and then overriden  
so a clear interface can be followed, my personal belief.

---

<div class="post-metadata">

**Author:** ![onurturgay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/ecc23a/32.png) [@onurturgay](https://www.ruby-forum.com/u/onurturgay)\
**Post date:** [December 13, 2005, 3:13pm UTC](https://www.ruby-forum.com/t/pass-by-reference-or-pass-by-value/49539/33 "2005-12-13T15:13:46Z")

</div>

On 7-dec-2005, at 13:46, Vivek K. wrote:

> puts m \>\> 4 (i want to change this to 5)  
> Ruby MOSTLY uses “pass by reference” (in PHP-speak), but it also  
> returns from the last expression (and you can use the returned result).

It’s not possible to change objects outside of scope with Fixnums  
(see the thread above) and surely not possible with Symbols, but  
possible with all other objects that support Object#bang! methods  
(actually all objects that support Object#replace)

def process\_text\_inside(text)  
text.gsub!(/a/, ‘b’)  
end

some\_text = ‘blabla’

process\_text\_inside(some\_text)

some\_text #=\> 'blbblb;

def replace\_text\_inside(text)  
text.replace(“foo”) # you have to pass an object of the same class,  
otherwise you get a TypeError  
end

replace\_text\_inside(some\_text)

some\_text #=\> ‘foo’

If you explicitly need to modify the passed value without modifying  
the passed variable (you will shoot yourself in the foot if you do so  
unintentionally), use Object#dup.

Please read PickAxe, you are missing some fundamental Ruby basics.  
And never name your variable statusCode.

–  
Julian ‘Julik’ Tarkhanov  
me at julik.nl

---

<div class="post-metadata">

**Author:** ![onurturgay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/ecc23a/32.png) [@onurturgay](https://www.ruby-forum.com/u/onurturgay)\
**Post date:** [September 8, 2008, 3:29pm UTC](https://www.ruby-forum.com/t/pass-by-reference-or-pass-by-value/49539/34 "2008-09-08T15:29:19Z")

</div>

Kazuyoshi T. wrote:

> I got it, I go it, I got it!!!
> 
> def change str  
> str.replace ‘…’  
> end
> 
> str = ‘.’  
> change str  
> puts str
> 
> #=\> …
> 
> # yey!

(Hey guys, I realize this thread is a month old, but its relevant to  
something I’m doing right now.)

Wow, this is the first time I’ve been disappointed by Ruby. This seems  
to me to be a _very_ poor choice. This (real code)…

string = ‘a string’  
copy = string  
string.replace ‘a different string’

…seems a very poor substitute for this (pseudocode), which I would  
expect to have the same result…

string = ‘a string’  
copy = \*string  
string = ‘a different string’

…because the real code requires the changer of the string to know that  
there is going to be a copy made of the string. Here’s a more realistic  
example (assume Person is a class that allows new instance variables to  
be created at runtime; in fact, I have completely implemented such a  
Person class and the irb I/O shown is completely real):

irb\> p = Person.new  
=\> #\<Person:0x2bc43d0 @field\_list=[:first\_name, :last\_name,  
:middle\_name, :location, :primary\_email, :primary\_phone\_number]\>  
irb\> p.personal\_email = ‘removed\_email\_address@domain.invalid’  
=\> “removed\_email\_address@domain.invalid”  
irb\> p  
=\> #\<Person:0x2bc43d0 @personal\_email=“removed\_email\_address@domain.invalid”,  
@field\_list=[:first\_name, :last\_name, :middle\_name, :location,  
:primary\_email, :primary\_phone\_number, :personal\_email]\>  
irb\> p.primary\_email = p.personal\_email  
=\> “removed\_email\_address@domain.invalid”  
irb\> p  
=\> #\<Person:0x2bc43d0 @personal\_email=“removed\_email\_address@domain.invalid”,  
@primary\_email=“removed\_email\_address@domain.invalid”, @field\_list=[:first\_name, :last\_name,  
:middle\_name, :location, :primary\_email, :primary\_phone\_number,  
:personal\_email]\>  
irb\> p.personal\_email.replace ‘removed\_email\_address@domain.invalid’  
=\> “removed\_email\_address@domain.invalid”  
irb\> p  
=\> #\<Person:0x2bc43d0 @personal\_email=“removed\_email\_address@domain.invalid”,  
@primary\_email=“removed\_email\_address@domain.invalid”, @field\_list=[:first\_name, :last\_name,  
:middle\_name, :location, :primary\_email, :primary\_phone\_number,  
:personal\_email]\>

Is there really no way to do this such that the line  
“p.personal\_email.replace ‘removed\_email\_address@domain.invalid’” can be replaced with  
“p.personal\_email = ‘removed\_email\_address@domain.invalid’” and both strings will be modified?  
I think that’s terrible–obviously, it’s pretty realistic to expect that  
code like this would occur, but making it possible it requires every  
piece of code that changes p.personal\_email to know that there is or  
could be a copy of it made. It makes String.= virtually useless,  
because in order to make it possible for a user to create a new String  
that always reflects the contents of another String, one must always use  
String.replace instead of String.= to assign values to Strings. It  
seems like the best solution would simply be to add an =\* operator (to  
Object itself, ideally) such that this would be possible, in place of  
the line “p.primary\_email = p.personal\_email” above:

p.primary\_email =\* p.personal\_email #p.primary\_email now contains a  
pointer directly to p.personal\_email instead of containing a pointer to  
the same object that p.personal\_email points to

Of course, that is easier said than done, but I can’t think of any other  
solution that comes close to being as elegant.

Am I missing something? Is there already a way to create pointers to  
pointers in Ruby so that this problem can be avoided?

Thanks,  
Doug

---

<div class="post-metadata">

**Author:** ![onurturgay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/ecc23a/32.png) [@onurturgay](https://www.ruby-forum.com/u/onurturgay)\
**Post date:** [January 10, 2015, 11:06am UTC](https://www.ruby-forum.com/t/pass-by-reference-or-pass-by-value/49539/35 "2015-01-10T11:06:08Z")

</div>

> > string = ‘a string’  
> > copy = string  
> > string.replace ‘a different string’

> > …because the real code requires the changer of the string to know that  
> > there is going to be a copy made of the string.

For me, Object#clone or Object#dup works:

string = ‘a string’  
copy = string.clone  
string = ‘a different string’

[Previous page](https://www.ruby-forum.com/t/pass-by-reference-or-pass-by-value/49539.md?page=1)
