# Value by reference

**URL:** <https://rubytalk.org/t/value-by-reference/15872>\
**Category:** ruby-talk\
**Created:** [18 January 2005 21:49 UTC](https://rubytalk.org/t/value-by-reference/15872 "2005-01-18T21:49:00Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mohammad\_Khan3](https://avatars.discourse-cdn.com/v4/letter/m/ea666f/32.png) [@Mohammad\_Khan3](https://rubytalk.org/u/Mohammad_Khan3)\
**Post date:** [18 January 2005 21:49 UTC](https://rubytalk.org/t/value-by-reference/15872/1 "2005-01-18T21:49:00Z")

</div>

Before telling my issue, let me show my script:

def do\_something(a, b, c)  
&nbsp;&nbsp;&nbsp;&nbsp;a = a + 1  
&nbsp;&nbsp;&nbsp;&nbsp;b = b + 2  
&nbsp;&nbsp;&nbsp;&nbsp;c = c + 3  
end  
a, b, c = 5, 6, 7  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb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p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "after do\_something:"  
puts "a : #{a}"  
puts "b : #{b}"  
puts "c : #{c}"

My issue:  
I would like to see the value of a, b, c after calling #do\_something as:  
a = 6, b = 8, c = 10

how can I do it without making a, b, c an instance variable?  
In other words, how can I send value by reference?

Thanks  
MOhammad

---

<div class="post-metadata">

**Author:** ![Trans1](https://avatars.discourse-cdn.com/v4/letter/t/ea5d25/32.png) [@Trans1](https://rubytalk.org/u/Trans1)\
**Post date:** [18 January 2005 22:11 UTC](https://rubytalk.org/t/value-by-reference/15872/2 "2005-01-18T22:11:04Z")

</div>

I beleive it is by reference, the problem is you need to modify  
inplace.

In this particular case you'll need an alternative to #+, b/c #+ is not  
an inplace operator. But will you use, I'm not sure. #succ seems to  
work inplace, but that's not to helpful. (I've a mind to RCR for a 'def  
succ(n=1)', but that doesn't help you now)

It does seem like there should be clearer ways to deal with this. I  
know .net for instance has very clear distinctions.

---

<div class="post-metadata">

**Author:** ![Florian\_Gross](https://avatars.discourse-cdn.com/v4/letter/f/e9a140/32.png) [@Florian\_Gross](https://rubytalk.org/u/Florian_Gross)\
**Post date:** [18 January 2005 22:16 UTC](https://rubytalk.org/t/value-by-reference/15872/3 "2005-01-18T22:16:04Z")

</div>

Mohammad Khan wrote:

> Before telling my issue, let me show my script:
> 
> def do\_something(a, b, c)  
> &nbsp;&nbsp;&nbsp;&nbsp;a = a + 1  
> &nbsp;&nbsp;&nbsp;&nbsp;b = b + 2  
> &nbsp;&nbsp;&nbsp;&nbsp;c = c + 3  
> end a, b, c = 5, 6, 7  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "before do\_something:"  
> I would like to see the value of a, b, c after calling #do\_something as:  
> a = 6, b = 8, c = 10
> 
> how can I do it without making a, b, c an instance variable?  
> In other words, how can I send value by reference?

This is not easily done. First of all you should ask yourself why you find yourself needing it and if there is not a better mechanism in Ruby that you could use to accomplish your goal. (Methods can return multiple values.)

After that there's two approaches to addressing the above:

1) Supply lambdas:

def do\_something(inc\_a, inc\_b, inc\_c)  
&nbsp;&nbsp;&nbsp;inc\_a.call(1)  
&nbsp;&nbsp;&nbsp;inc\_b.call(1)  
&nbsp;&nbsp;&nbsp;inc\_c.call(1)  
end

do\_something(  
&nbsp;&nbsp;&nbsp;lambda { |x| a += x },  
&nbsp;&nbsp;&nbsp;lambda { |x| b += x },  
&nbsp;&nbsp;&nbsp;lambda { |x| c += x }  
)

2) Use variable.rb to handle variables as Objects:

def do\_something(a, b, c)  
&nbsp;&nbsp;&nbsp;a.value += 1  
&nbsp;&nbsp;&nbsp;b.value += 1  
&nbsp;&nbsp;&nbsp;c.value += 1  
end

do\_something(Variable[:a], Variable[:b], Variable[:c])

If you find yourself really needing to do this, please follow up with your reasons. I've not yet found many cases where this is necessary and would be interested in getting to know about new ones.

[variable.rb](https://rubytalk.org/uploads/short-url/fsJpgssYh6FLDQjSX9fKdtTHQet.rb) (1000 Bytes)

---

<div class="post-metadata">

**Author:** ![Assaph\_Mehr1](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@Assaph\_Mehr1](https://rubytalk.org/u/Assaph_Mehr1)\
**Post date:** [18 January 2005 22:26 UTC](https://rubytalk.org/t/value-by-reference/15872/4 "2005-01-18T22:26:03Z")

</div>

It's probably easier if you're used to think in pointers / references.  
Ignore for the moment that fixnums are special (immediate values).

> def do\_something(a, b, c)  
> &nbsp;&nbsp;&nbsp;&nbsp;a = a + 1  
> &nbsp;&nbsp;&nbsp;&nbsp;b = b + 2  
> &nbsp;&nbsp;&nbsp;&nbsp;c = c + 3  
> end

do\_something receives 3 references in the declaration. those references  
are places in three method-local variables.  
in the body, it changes the local-variables to references that point to  
\*other\* objects.

> a, b, c = 5, 6, 7

you have created three local variables, referencing fixnum objects.

> puts "before do\_something:"  
> puts "a : #{a}"  
> puts "b : #{b}"  
> puts "c : #{c}"

you print the values of the objects referenced by the local variables.

> do\_something(a, b, c)

you called do\_something, which received the references, and changed \*in  
the method body only\* the method-local variables to point to other  
objects.

> puts "after do\_something:"  
> puts "a : #{a}"  
> puts "b : #{b}"  
> puts "c : #{c}"

here you print the values referenced by the outside-scope local  
variables. these local-variables have never been changed in this scope  
so they point to the same fixnum objects you specified in the  
beginning.

> My issue:  
> I would like to see the value of a, b, c after calling #do\_something

as:

> a = 6, b = 8, c = 10
> 
> how can I do it without making a, b, c an instance variable?  
> In other words, how can I send value by reference?

you have some options:  
- global variables ($var): ugly and not recommended.  
- return the new values: probably cleanest way.

def do\_something(a, b, c)  
a = a + 1  
b = b + 2  
c = c + 3  
[a, b, c]  
end  
a, b, c = 5, 6, 7  
a, b, c = do\_something(a, b, c)

- create a box class: essentially a pointer. both the ouside-scope and  
the method-scope local-variables point to the same box object so you  
can change fields in the box object and those changes will be reflected  
in all scopes.

HTH,  
Assaph

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [19 January 2005 01:38 UTC](https://rubytalk.org/t/value-by-reference/15872/5 "2005-01-19T01:38:47Z")

</div>

Hi --

> Before telling my issue, let me show my script:
> 
> def do\_something(a, b, c)  
> &nbsp;&nbsp;&nbsp;a = a + 1  
> &nbsp;&nbsp;&nbsp;b = b + 2  
> &nbsp;&nbsp;&nbsp;c = c + 3  
> end  
> a, b, c = 5, 6, 7  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs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&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "after do\_something:"  
> puts "a : #{a}"  
> puts "b : #{b}"  
> puts "c : #{c}"
> 
> My issue:  
> I would like to see the value of a, b, c after calling #do\_something as:  
> a = 6, b = 8, c = 10
> 
> how can I do it without making a, b, c an instance variable?  
> In other words, how can I send value by reference?

Fixnums are immediate values, and are immutable, even when they're  
wrapped in variables. So what you're trying to do is equivalent to:

&nbsp;&nbsp;&nbsp;do\_something(5,6,7)

&nbsp;&nbsp;&nbsp;puts 5 \* 5 # 36 -- i.e., 5 has become 6

It won't work (luckily 🙂 because Ruby doesn't let you perform that  
kind of modification on Fixnums.

With mutable objects you can do what you're describing -- for example,  
adding to a string that's in a local variable:

&nbsp;&nbsp;&nbsp;irb(main):001:0\> def x(y); y \<\< "def"; end  
&nbsp;&nbsp;&nbsp;=\> nil  
&nbsp;&nbsp;&nbsp;irb(main):002:0\> s = "abc"  
&nbsp;&nbsp;&nbsp;=\> "abc"  
&nbsp;&nbsp;&nbsp;irb(main):003:0\> x(s)  
&nbsp;&nbsp;&nbsp;=\> "abcdef"  
&nbsp;&nbsp;&nbsp;irb(main):004:0\> s  
&nbsp;&nbsp;&nbsp;=\> "abcdef"

David

> **···**
>
> On Wed, 19 Jan 2005, Mohammad Khan wrote:
> 
> --  
> David A. Black  
> dblack@wobblini.net

---

<div class="post-metadata">

**Author:** ![Mohammad\_Khan3](https://avatars.discourse-cdn.com/v4/letter/m/ea666f/32.png) [@Mohammad\_Khan3](https://rubytalk.org/u/Mohammad_Khan3)\
**Post date:** [19 January 2005 16:10 UTC](https://rubytalk.org/t/value-by-reference/15872/6 "2005-01-19T16:10:22Z")

</div>

#!/usr/bin/env ruby

def do\_something(a, b, c)  
&nbsp;&nbsp;a += 1  
&nbsp;&nbsp;b += 2  
&nbsp;&nbsp;c += 3  
end

def another\_do\_something(foo=Foo.new)  
&nbsp;&nbsp;foo.do\_something  
end

class Foo  
&nbsp;&nbsp;def initialize  
&nbsp;&nbsp;&nbsp;&nbsp;@value = 0  
&nbsp;&nbsp;end

&nbsp;&nbsp;def value  
&nbsp;&nbsp;&nbsp;&nbsp;return @value  
&nbsp;&nbsp;end

&nbsp;&nbsp;def value=(val)  
&nbsp;&nbsp;&nbsp;&nbsp;@value = val  
&nbsp;&nbsp;end

&nbsp;&nbsp;def do\_something  
&nbsp;&nbsp;&nbsp;&nbsp;@value += 1  
&nbsp;&nbsp;&nbsp;&nbsp;@value += 2  
&nbsp;&nbsp;&nbsp;&nbsp;@value += 3  
&nbsp;&nbsp;end  
end

a, b, c = 5, 6, 7  
f = Foo.new

# scenario 1  
puts "before do\_something : #{a.inspect}, #{b.inspect}, #{c.inspect}"  
do\_something( a, b, c)  
puts "after do\_something : #{a.inspect}, #{b.inspect}, #{c.inspect}" # a, b, c not changed  
#do\_something( a.clone, b.clone, c.clone)  
#puts "after clone : #{a.inspect}, #{b.inspect}, #{c.inspect}" # can't clone Fixnum

# scenario 2  
puts "before : #{f.inspect}"  
another\_do\_something(f)  
puts "after with f : #{f.inspect}" # f changed  
another\_do\_something(f.clone)  
puts "after with f.clone : #{f.inspect}" # f not changed, it make sense

What is the difference between scenario 1 and scenario 2?  
What is making scenario 1 not changed?

If everything is object, Why can't I do  
do\_something(a.clone, b.clone, c.clone) # I don't want them to be changed  
do\_something(a, b, c) # change a, b, c like other objects

Hope, I could explain properly what I am trying to understand.

MOhammad

---

<div class="post-metadata">

**Author:** ![Mohammad\_Khan3](https://avatars.discourse-cdn.com/v4/letter/m/ea666f/32.png) [@Mohammad\_Khan3](https://rubytalk.org/u/Mohammad_Khan3)\
**Post date:** [19 January 2005 19:47 UTC](https://rubytalk.org/t/value-by-reference/15872/7 "2005-01-19T19:47:19Z")

</div>

What I was missing, this thread made me to understand that.

a = 5  
puts a.class -\> Fixnum

As I knew everything is object in Ruby.. I thought a = 5 would do something like Fixnum.new(5) internally.  
and 'a' would remain as object. I was wrong at this point.

Thanks everybody, specially Florian and Austin for their nice explanation.

I am out of this tread.

Mohammad

---

<div class="post-metadata">

**Author:** ![Mohammad\_Khan3](https://avatars.discourse-cdn.com/v4/letter/m/ea666f/32.png) [@Mohammad\_Khan3](https://rubytalk.org/u/Mohammad_Khan3)\
**Post date:** [18 January 2005 22:37 UTC](https://rubytalk.org/t/value-by-reference/15872/8 "2005-01-18T22:37:42Z")

</div>

> Mohammad Khan wrote:
> 
> \> Before telling my issue, let me show my script:  
> \>  
> \> def do\_something(a, b, c)  
> \> a = a + 1  
> \> b = b + 2  
> \> c = c + 3  
> \> end  
> \> a, b, c = 5, 6, 7  
> \> puts "before do\_something:"  
> \> I would like to see the value of a, b, c after calling #do\_something as:  
> \> a = 6, b = 8, c = 10  
> \>  
> \> how can I do it without making a, b, c an instance variable?  
> \> In other words, how can I send value by reference?
> 
> This is not easily done. First of all you should ask yourself why you  
> find yourself needing it and if there is not a better mechanism in Ruby  
> that you could use to accomplish your goal. (Methods can return multiple  
> values.)

I really need it.  
I could do it by:

def do\_something(a, b, c)  
&nbsp;&nbsp;a = a + 1  
&nbsp;&nbsp;b = b + 2  
&nbsp;&nbsp;c = c + 3  
&nbsp;&nbsp;return [a , b, c]  
end

a, b, c = do\_something(a, b, c)

I don't like to handle with too many return variables but I had to do it  
in my real project.

> After that there's two approaches to addressing the above:
> 
> 1) Supply lambdas:
> 
> def do\_something(inc\_a, inc\_b, inc\_c)  
> &nbsp;&nbsp;&nbsp;inc\_a.call(1)  
> &nbsp;&nbsp;&nbsp;inc\_b.call(1)  
> &nbsp;&nbsp;&nbsp;inc\_c.call(1)  
> end
> 
> do\_something(  
> &nbsp;&nbsp;&nbsp;lambda { |x| a += x },  
> &nbsp;&nbsp;&nbsp;lambda { |x| b += x },  
> &nbsp;&nbsp;&nbsp;lambda { |x| c += x }  
> )

What is lambdas? sorry.. if I am asking anything very strange !!  
I will still prefer to return multiple variables in stead of using these  
much code!

> **···**
>
> On Tue, 2005-01-18 at 17:16, Florian Gross wrote:
> 
> > 2) Use variable.rb to handle variables as Objects:
> > 
> > def do\_something(a, b, c)  
> > &nbsp;&nbsp;&nbsp;a.value += 1  
> > &nbsp;&nbsp;&nbsp;b.value += 1  
> > &nbsp;&nbsp;&nbsp;c.value += 1  
> > end
> > 
> > do\_something(Variable[:a], Variable[:b], Variable[:c])
> > 
> > If you find yourself really needing to do this, please follow up with  
> > your reasons. I've not yet found many cases where this is necessary and  
> > would be interested in getting to know about new ones.

---

<div class="post-metadata">

**Author:** ![Trans1](https://avatars.discourse-cdn.com/v4/letter/t/ea5d25/32.png) [@Trans1](https://rubytalk.org/u/Trans1)\
**Post date:** [19 January 2005 00:36 UTC](https://rubytalk.org/t/value-by-reference/15872/9 "2005-01-19T00:36:04Z")

</div>

I would like to elaborate on this generally. Is ti possibel to have a  
clean distinction? In other words, would to be possible to have all  
objects to be modifiable inplace somehow? I know there are pathces out  
there that do this, but what keeps this from being generally  
acceptable?

T.

---

<div class="post-metadata">

**Author:** ![Trans1](https://avatars.discourse-cdn.com/v4/letter/t/ea5d25/32.png) [@Trans1](https://rubytalk.org/u/Trans1)\
**Post date:** [19 January 2005 03:16 UTC](https://rubytalk.org/t/value-by-reference/15872/10 "2005-01-19T03:16:02Z")

</div>

Sorry. #succ does not act inplace and nothing does on Fixnum. Its been  
a while since I used [VB.Net](http://VB.Net) but in it you can define a subroutine with  
parameters ByVal or ByRef:

Private Sub IntegerByRef(ByRef X As Integer)  
Dim i As Long  
For i = 1 To m\_NumAssignments  
X = 123  
Next i  
End Sub

Private Sub IntegerByVal(ByVal X As Integer)  
Dim i As Long  
For i = 1 To m\_NumAssignments  
X = 123  
Next i  
End Sub

Those are the two ways in VB, but Ruby is sort of inbetween. It passes  
by reference, but if you reassign it looses the reference. You can  
simulate by value simply by duplicating the parameter when it comes in,  
but to do the other requires some trickery. One way is:

def do\_somthing(a,b,c)  
a[0] += 1  
b[0] += 1  
c[0] += 1  
end  
a, b, c = [5], [6], [7]  
do\_something(a,b,c)

But I wonder, could Ruby offer something like the VB forms without  
violating immutability? Sort of an indirect reference.  
Guess I don't understand why it's conidered a negative.

T.

---

<div class="post-metadata">

**Author:** ![Florian\_Gross](https://avatars.discourse-cdn.com/v4/letter/f/e9a140/32.png) [@Florian\_Gross](https://rubytalk.org/u/Florian_Gross)\
**Post date:** [19 January 2005 17:01 UTC](https://rubytalk.org/t/value-by-reference/15872/11 "2005-01-19T17:01:01Z")

</div>

Mohammad Khan wrote:

> do\_something(a, b, c) # change a, b, c like other objects

Let me give an example:

x, y, z = 1, 2, 3  
do\_something(x, y, z) # completely equivalent to  
do\_something(1, 2, 3)

do\_something() does not get variables. It gets the values the variables refer to. (It gets the objects the variables are names for.)

Therefore it can not change the variables, they are nothing physical.

variable.rb lets you turn variables \*themselves\* into Objects that can be passed to methods.

---

<div class="post-metadata">

**Author:** ![Austin\_Ziegler5](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/austin_ziegler5/32/1985_2.png) [@Austin\_Ziegler5](https://rubytalk.org/u/Austin_Ziegler5)\
**Post date:** [19 January 2005 17:05 UTC](https://rubytalk.org/t/value-by-reference/15872/12 "2005-01-19T17:05:51Z")

</div>

> def do\_something(a, b, c)  
> &nbsp;&nbsp;a += 1  
> &nbsp;&nbsp;b += 2  
> &nbsp;&nbsp;c += 3  
> end
> 
> def another\_do\_something(foo = Foo.new)  
> &nbsp;&nbsp;foo.do\_something  
> end
> 
> class Foo  
> &nbsp;&nbsp;def initialize  
> &nbsp;&nbsp;&nbsp;&nbsp;@value = 0  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;def value  
> &nbsp;&nbsp;&nbsp;&nbsp;return @value  
> &nbsp;&nbsp;end  
> &nbsp;&nbsp;  
> &nbsp;&nbsp;def value=(val)  
> &nbsp;&nbsp;&nbsp;&nbsp;@value = val  
> &nbsp;&nbsp;end  
> &nbsp;&nbsp;  
> &nbsp;&nbsp;def do\_something  
> &nbsp;&nbsp;&nbsp;&nbsp;@value += 1  
> &nbsp;&nbsp;&nbsp;&nbsp;@value += 2  
> &nbsp;&nbsp;&nbsp;&nbsp;@value += 3  
> &nbsp;&nbsp;end  
> end

> a, b, c = 5, 6, 7  
> f = Foo.new

[...]

> If everything is object, Why can't I do  
> &nbsp;&nbsp;do\_something(a.clone, b.clone, c.clone) # I don't want them to  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# be changed  
> &nbsp;&nbsp;do\_something(a, b, c) # change a, b, c like  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# other objects  
> Hope, I could explain properly what I am trying to understand.

I don't know why you're not getting this, as it is very simple.

a, b, and c are not objects. They're variables.

When you call Foo#do\_something, you're changing the internal state  
of an object. When you call Object#do\_something, you're passing  
three variables that can refer to just about any object.

It's no different than if I defined do\_something as:

&nbsp;&nbsp;def do\_something(a, b, c)  
&nbsp;&nbsp;&nbsp;&nbsp;a += Foo.new  
&nbsp;&nbsp;&nbsp;&nbsp;b += Foo.new  
&nbsp;&nbsp;&nbsp;&nbsp;c += Foo.new  
&nbsp;&nbsp;end

As long as Foo can be added to the values of a, b, and c, I'm going  
to get three completely new objects.

Try it with this:

&nbsp;&nbsp;def do\_something(a, b, c)  
&nbsp;&nbsp;&nbsp;&nbsp;a += "-half"  
&nbsp;&nbsp;&nbsp;&nbsp;b += "-thirds"  
&nbsp;&nbsp;&nbsp;&nbsp;c += "-fourths"  
&nbsp;&nbsp;&nbsp;&nbsp;puts a.id, b.id, c.id  
&nbsp;&nbsp;&nbsp;&nbsp;[a, b, c]  
&nbsp;&nbsp;end

&nbsp;&nbsp;def do\_something2(a, b, c)  
&nbsp;&nbsp;&nbsp;&nbsp;a \<\< "-half"  
&nbsp;&nbsp;&nbsp;&nbsp;b \<\< "-thirds"  
&nbsp;&nbsp;&nbsp;&nbsp;c \<\< "-fourths"  
&nbsp;&nbsp;&nbsp;&nbsp;puts a.id, b.id, c.id  
&nbsp;&nbsp;&nbsp;&nbsp;[a, b, c]  
&nbsp;&nbsp;end

&nbsp;&nbsp;a = "one"  
&nbsp;&nbsp;b = "two"  
&nbsp;&nbsp;c = "three"

&nbsp;&nbsp;puts "#{a}\<#{a.id}\>, #{b}\<#{b.id}\>, #{c}\<#{c.id}\>"

&nbsp;&nbsp;x, y, z = do\_something(a, b, c)  
&nbsp;&nbsp;puts "#{x}\<#{x.id}\>, #{y}\<#{y.id}\>, #{z}\<#{z.id}\>"  
&nbsp;&nbsp;puts "#{a}\<#{a.id}\>, #{b}\<#{b.id}\>, #{c}\<#{c.id}\>"

&nbsp;&nbsp;x, y, z = do\_something2(a, b, c)  
&nbsp;&nbsp;puts "#{a}\<#{a.id}\>, #{b}\<#{b.id}\>, #{c}\<#{c.id}\>"  
&nbsp;&nbsp;puts "#{x}\<#{x.id}\>, #{y}\<#{y.id}\>, #{z}\<#{z.id}\>"

This will help you understand. #\<\< modifies a String in-place. It's  
concatenation. += concatenates and creates a new string.

-austin

> **···**
>
> On Thu, 20 Jan 2005 01:10:22 +0900, Mohammad Khan \<mkhan@lextranet.com\> wrote:  
> --  
> Austin Ziegler \* halostatue@gmail.com  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\* Alternate: austin@halostatue.ca

---

<div class="post-metadata">

**Author:** ![Florian\_Gross](https://avatars.discourse-cdn.com/v4/letter/f/e9a140/32.png) [@Florian\_Gross](https://rubytalk.org/u/Florian_Gross)\
**Post date:** [18 January 2005 23:16 UTC](https://rubytalk.org/t/value-by-reference/15872/13 "2005-01-18T23:16:03Z")

</div>

Mohammad Khan wrote:

> def do\_something(a, b, c)  
> &nbsp;&nbsp;a = a + 1  
> &nbsp;&nbsp;b = b + 2  
> &nbsp;&nbsp;c = c + 3  
> &nbsp;&nbsp;return [a , b, c]  
> end
> 
> a, b, c = do\_something(a, b, c)
> 
> I don't like to handle with too many return variables but I had to do it  
> in my real project.

Maybe you could return a Hash then?

> > 1) Supply lambdas:
> > 
> > do\_something(  
> > &nbsp;&nbsp;lambda { |x| a += x },  
> > &nbsp;&nbsp;lambda { |x| b += x },  
> > &nbsp;&nbsp;lambda { |x| c += x }  
> > )
> 
> What is lambdas? sorry.. if I am asking anything very strange !!  
> I will still prefer to return multiple variables in stead of using these  
> much code!

lambdas are objectified blocks. You can pass them around, assign them to variables, call methods and more of the stuff you can do with Objects.

The above might look like much code, but there's millions way of making it shorter. Here's one:

do\_something { |ai, bi, ci| a += ai; b += bi; c += ci }

Invokation in the method would change to yield(1, 1, 1).

> > If you find yourself really needing to do this, please follow up with your reasons. I've not yet found many cases where this is necessary and would be interested in getting to know about new ones.

I'm still interested in your reasons for doing this. 🙂

---

<div class="post-metadata">

**Author:** ![Florian\_Gross](https://avatars.discourse-cdn.com/v4/letter/f/e9a140/32.png) [@Florian\_Gross](https://rubytalk.org/u/Florian_Gross)\
**Post date:** [19 January 2005 12:16 UTC](https://rubytalk.org/t/value-by-reference/15872/14 "2005-01-19T12:16:09Z")

</div>

trans. wrote:

> Sorry. #succ does not act inplace and nothing does on Fixnum.

Of course nothing can change the value of immediate Objects. 🙂

> But I wonder, could Ruby offer something like the VB forms without  
> violating immutability? Sort of an indirect reference.  
> Guess I don't understand why it's conidered a negative.

See my earlier posting about the lambda { } and Variable[:name] ways of doing this.

Ruby itself just treats variables as names for Objects. You can't really refer to a variable itself, you can only refer to the Object it is a name for. That's a surprisingly simple and non-confusing model. I think Ruby itself should not be changed. (Though it would be nice if it had something like Binding.of\_caller built-in...)

---

<div class="post-metadata">

**Author:** ![Mohammad\_Khan3](https://avatars.discourse-cdn.com/v4/letter/m/ea666f/32.png) [@Mohammad\_Khan3](https://rubytalk.org/u/Mohammad_Khan3)\
**Post date:** [19 January 2005 17:14 UTC](https://rubytalk.org/t/value-by-reference/15872/15 "2005-01-19T17:14:59Z")

</div>

I understand that.  
But what about scenario 2?  
Scenario 2 is not acting as scenario 1, is it because scenario 2 gets  
object rather than variable?  
Or, Scenario 2 gets clonable object while scenario 1 not?  
Thanks, Florian.

> **···**
>
> On Wed, 2005-01-19 at 12:01, Florian Gross wrote:
> 
> > Mohammad Khan wrote:
> > 
> > \> do\_something(a, b, c) # change a, b, c like other objects
> > 
> > Let me give an example:
> > 
> > x, y, z = 1, 2, 3  
> > do\_something(x, y, z) # completely equivalent to  
> > do\_something(1, 2, 3)
> > 
> > do\_something() does not get variables. It gets the values the variables  
> > refer to. (It gets the objects the variables are names for.)
> 
> --  
> Mohammad
> 
> > Therefore it can not change the variables, they are nothing physical.
> > 
> > variable.rb lets you turn variables \*themselves\* into Objects that can  
> > be passed to methods.

---

<div class="post-metadata">

**Author:** ![Mohammad\_Khan3](https://avatars.discourse-cdn.com/v4/letter/m/ea666f/32.png) [@Mohammad\_Khan3](https://rubytalk.org/u/Mohammad_Khan3)\
**Post date:** [19 January 2005 17:17 UTC](https://rubytalk.org/t/value-by-reference/15872/16 "2005-01-19T17:17:19Z")

</div>

> \> def do\_something(a, b, c)  
> \> a += 1  
> \> b += 2  
> \> c += 3  
> \> end  
> \>  
> \> def another\_do\_something(foo = Foo.new)  
> \> foo.do\_something  
> \> end  
> \>  
> \> class Foo  
> \> def initialize  
> \> @value = 0  
> \> end  
> \>  
> \> def value  
> \> return @value  
> \> end  
> \>  
> \> def value=(val)  
> \> @value = val  
> \> end  
> \>  
> \> def do\_something  
> \> @value += 1  
> \> @value += 2  
> \> @value += 3  
> \> end  
> \> end
> 
> \> a, b, c = 5, 6, 7  
> \> f = Foo.new  
> [...]  
> \> If everything is object, Why can't I do  
> \> do\_something(a.clone, b.clone, c.clone) # I don't want them to  
> \> # be changed  
> \> do\_something(a, b, c) # change a, b, c like  
> \> # other objects  
> \> Hope, I could explain properly what I am trying to understand.
> 
> I don't know why you're not getting this, as it is very simple.

May be I am not that simple!! 😛

> a, b, and c are not objects. They're variables.

Isn't everything \*object\* in Ruby?

Now you know, what I am after ?!

> **···**
>
> On Wed, 2005-01-19 at 12:05, Austin Ziegler wrote:
> 
> > On Thu, 20 Jan 2005 01:10:22 +0900, Mohammad Khan \<mkhan@lextranet.com\> wrote:
> 
> --  
> MOhammad
> 
> > When you call Foo#do\_something, you're changing the internal state  
> > of an object. When you call Object#do\_something, you're passing  
> > three variables that can refer to just about any object.
> > 
> > It's no different than if I defined do\_something as:
> > 
> > &nbsp;&nbsp;def do\_something(a, b, c)  
> > &nbsp;&nbsp;&nbsp;&nbsp;a += Foo.new  
> > &nbsp;&nbsp;&nbsp;&nbsp;b += Foo.new  
> > &nbsp;&nbsp;&nbsp;&nbsp;c += Foo.new  
> > &nbsp;&nbsp;end
> > 
> > As long as Foo can be added to the values of a, b, and c, I'm going  
> > to get three completely new objects.
> > 
> > Try it with this:
> > 
> > &nbsp;&nbsp;def do\_something(a, b, c)  
> > &nbsp;&nbsp;&nbsp;&nbsp;a += "-half"  
> > &nbsp;&nbsp;&nbsp;&nbsp;b += "-thirds"  
> > &nbsp;&nbsp;&nbsp;&nbsp;c += "-fourths"  
> > &nbsp;&nbsp;&nbsp;&nbsp;puts a.id, b.id, c.id  
> > &nbsp;&nbsp;&nbsp;&nbsp;[a, b, c]  
> > &nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;def do\_something2(a, b, c)  
> > &nbsp;&nbsp;&nbsp;&nbsp;a \<\< "-half"  
> > &nbsp;&nbsp;&nbsp;&nbsp;b \<\< "-thirds"  
> > &nbsp;&nbsp;&nbsp;&nbsp;c \<\< "-fourths"  
> > &nbsp;&nbsp;&nbsp;&nbsp;puts a.id, b.id, c.id  
> > &nbsp;&nbsp;&nbsp;&nbsp;[a, b, c]  
> > &nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;a = "one"  
> > &nbsp;&nbsp;b = "two"  
> > &nbsp;&nbsp;c = "three"
> > 
> > &nbsp;&nbsp;puts "#{a}\<#{a.id}\>, #{b}\<#{b.id}\>, #{c}\<#{c.id}\>"
> > 
> > &nbsp;&nbsp;x, y, z = do\_something(a, b, c)  
> > &nbsp;&nbsp;puts "#{x}\<#{x.id}\>, #{y}\<#{y.id}\>, #{z}\<#{z.id}\>"  
> > &nbsp;&nbsp;puts "#{a}\<#{a.id}\>, #{b}\<#{b.id}\>, #{c}\<#{c.id}\>"
> > 
> > &nbsp;&nbsp;x, y, z = do\_something2(a, b, c)  
> > &nbsp;&nbsp;puts "#{a}\<#{a.id}\>, #{b}\<#{b.id}\>, #{c}\<#{c.id}\>"  
> > &nbsp;&nbsp;puts "#{x}\<#{x.id}\>, #{y}\<#{y.id}\>, #{z}\<#{z.id}\>"
> > 
> > This will help you understand. #\<\< modifies a String in-place. It's  
> > concatenation. += concatenates and creates a new string.
> > 
> > -austin

---

<div class="post-metadata">

**Author:** ![Glenn\_Parker1](https://avatars.discourse-cdn.com/v4/letter/g/ba8739/32.png) [@Glenn\_Parker1](https://rubytalk.org/u/Glenn_Parker1)\
**Post date:** [19 January 2005 14:03 UTC](https://rubytalk.org/t/value-by-reference/15872/17 "2005-01-19T14:03:59Z")

</div>

Florian Gross wrote:

> See my earlier posting about the lambda { } and Variable[:name] ways of doing this.

Sorry, but I can't believe anybody would actually want to use the lambda styles as proposed. They are ugly and obscure.

> Ruby itself just treats variables as names for Objects. You can't really refer to a variable itself, you can only refer to the Object it is a name for. That's a surprisingly simple and non-confusing model.

Judging by the frequency that this issue is discussed, it's more confusing than you suggest. A typical programmers expects call-by-reference to work one way, and Ruby works differently. Extra confusion results because this difference is masked by using self-updating methods, but it always fails for immediate objects, and it eventually fails in a surprising way for non-immediate objects. I call it surprising because a typical programmer does not expect the assignment operator to destroy call-by-reference, but that is exactly what happens.

&nbsp;&nbsp;&nbsp;def inc1(a, i); a += i; end  
&nbsp;&nbsp;&nbsp;def inc2(a, i); a = a + i; end

To the naive Ruby programmer, inc1 and inc2 seem to be equivalent, but Ruby gurus just shake their heads and sigh while they explain, yet again, that there is a difference. Why is this considered a good thing? 🙂

You might say that the typical programmer has grown accustomed to a confusing model, and Ruby is better off without that model. But, that is nigh on saying that Ruby is better off without the typical programmer. And that would be a shame, since Ruby has much to offer the hordes of Perl and Python programmers looking for something better.

Ruby does \*not\* support call-by-reference, in the traditional sense of the term. Instead, it is strictly call-by-value, and formal paremeters are copies of references from the calling scope. If Ruby had real call-by-reference, then it would be trivial to define a procedure that updates a variable from its parent's scope, and inc1 and inc2 above would be equivalent.

> I think Ruby itself should not be changed. (Though it would be nice if it had something like Binding.of\_caller built-in...)

Tcl is similar to Ruby, regarding both the style of variable passing and the resulting confusion among its new adopters, but Tcl documentation has always stressed the details of this issue, and the Tcl core has the "uplevel" and "upvar" builtins. I'm not a huge fan of Tcl, but I do think the Tcl folks handled this issue in the best way possible.

> **···**
>
> --  
> Glenn Parker | glenn.parker-AT-comcast.net | \<[http://www.tetrafoil.com/&gt](http://www.tetrafoil.com/&gt);

---

<div class="post-metadata">

**Author:** ![Trans1](https://avatars.discourse-cdn.com/v4/letter/t/ea5d25/32.png) [@Trans1](https://rubytalk.org/u/Trans1)\
**Post date:** [19 January 2005 14:06 UTC](https://rubytalk.org/t/value-by-reference/15872/18 "2005-01-19T14:06:02Z")

</div>

Right. I'm not suggesting that Ruby change this. I'm just wondering if  
might be possible to add an extra feature that would allow for the  
alternate when passing arguments through methods.

Personally, I've alwasy thought of variables as containters, so in that  
way of thinking, which I think is common, one would expect to be able  
to change what's in the container. I don't think Ruby's approach is  
always simple for the enduser and it does lead to some surprises --for  
example this very thread.

Hmm..reminds me of why I like the idea of all variables being  
collections....

Binding.of\_caller is useful for meta-programming, but that's bad  
business for general use.

T.

---

<div class="post-metadata">

**Author:** ![Florian\_Gross](https://avatars.discourse-cdn.com/v4/letter/f/e9a140/32.png) [@Florian\_Gross](https://rubytalk.org/u/Florian_Gross)\
**Post date:** [19 January 2005 17:31 UTC](https://rubytalk.org/t/value-by-reference/15872/19 "2005-01-19T17:31:01Z")

</div>

Mohammad Khan wrote:

> But what about scenario 2? [methods changes state of an Object that  
> it gets as an argument] Scenario 2 is not acting as scenario 1, is it  
> because scenario 2 gets object rather than variable? Or, Scenario 2  
> gets clonable object while scenario 1 not?

First, let's agree on using the term "mutable" (or "changeable") instead  
of "cloneable". (Immutable objects can not be cloned most of the time,  
but the both terms are not strictly the same things.)

Scenario 2 changes attributes of an Object. This is something that is  
entirely possible. It makes sense for a CarFactory object to be stopped  
or running -- it also makes sense that you are able to stop or (re)start  
such an object at run time. What can not be done is changing the value  
of a variable that appeared in the method invocation because the method  
does not know anything about the variable -- it gets the Object. Objects  
can have state that can be changed. This can happen via accessors that  
are invoked like obj.foo = x or other methods like car\_factory.stop.  
But while obj.foo looks similar to variable assignment it is something  
entirely else.

Why do different things look so similar in Ruby? It's uniform access at  
work.

In other languages there usually is the distinction between Object's  
fields and methods. Having public fields is usually a bad idea (it  
breaks encapsulation) and looks like this:

&nbsp;&nbsp;&nbsp;obj.field += 5

Having methods that modify state looks like this:

&nbsp;&nbsp;&nbsp;obj.setField(obj.getField() + 5)

In Ruby both look like this:

&nbsp;&nbsp;&nbsp;obj.field += 5

Even if obj.field=() or obj.field() do not directly map to an instance  
variable at all like in this case:

&nbsp;&nbsp;&nbsp;def obj.radius()  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@diameter / 2  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;def obj.radius=(new\_radius)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@diameter = new\_radius \* 2  
&nbsp;&nbsp;&nbsp;end

I hope this explains how Ruby works. If there is still anything left  
that you don't understand, feel free to ask.

---

<div class="post-metadata">

**Author:** ![Florian\_Gross](https://avatars.discourse-cdn.com/v4/letter/f/e9a140/32.png) [@Florian\_Gross](https://rubytalk.org/u/Florian_Gross)\
**Post date:** [19 January 2005 17:36 UTC](https://rubytalk.org/t/value-by-reference/15872/20 "2005-01-19T17:36:01Z")

</div>

Mohammad Khan wrote:

> > a, b, and c are not objects. They're variables.
> 
> Isn't everything \*object\* in Ruby?
> 
> Now you know, what I am after ?!

Every thing is an Object. Names for things are not Objects because then it would not be clear whether you are doing something with the name itself or with the Object it refers to. My variable.rb code lets you refer to the names themselves instead of their value with the Variable[:x] syntax.

Perl and PHP IIRC do this with the \$foo syntax (and C# AFAIK uses the 'ref' keyword for this, but don't quote me on this), but I think we should not have something like that in Ruby.

[Next page](https://rubytalk.org/t/value-by-reference/15872.md?page=2)
