# How to use a passing argument(returned argument)?

**URL:** <https://www.ruby-forum.com/t/how-to-use-a-passing-argument-returned-argument/154168>\
**Category:** Ruby\
**Created:** [November 26, 2008, 2:55am UTC](https://www.ruby-forum.com/t/how-to-use-a-passing-argument-returned-argument/154168 "2008-11-26T02:55:05Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![SSSS\_SSS\_SSS\_SSSSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7ba0ec/32.png) [@SSSS\_SSS\_SSS\_SSSSSS](https://www.ruby-forum.com/u/SSSS_SSS_SSS_SSSSSS)\
**Post date:** [November 26, 2008, 2:55am UTC](https://www.ruby-forum.com/t/how-to-use-a-passing-argument-returned-argument/154168/1 "2008-11-26T02:55:05Z")

</div>

Hello, everyone !!!

I have question about how to use a passing argument. firstly let me  
show sample code.

a = 0

def set10(aArg)  
aArg = 10  
end

set10(a)

p a \<— I wanna get 10 as a result.

How can I do for this one?

---

<div class="post-metadata">

**Author:** ![SSSS\_SSS\_SSS\_SSSSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7ba0ec/32.png) [@SSSS\_SSS\_SSS\_SSSSSS](https://www.ruby-forum.com/u/SSSS_SSS_SSS_SSSSSS)\
**Post date:** [November 26, 2008, 5:24am UTC](https://www.ruby-forum.com/t/how-to-use-a-passing-argument-returned-argument/154168/2 "2008-11-26T05:24:45Z")

</div>

On Nov 25, 2008, at 8:49 PM, ê¹€ ì¤€ì˜ wrote:

> Hello, everyone !!!

Hi there!

> p a \<— I wanna get 10 as a result.
> 
> How can I do for this one?

First, the pattern you are looking for is not very object oriented-y.  
That is, you’re asking to remove some of the logic pertaining to ‘a’  
and put it somewhere outside of ‘a’. Since ‘a’ is an object  
(everything in Ruby is), then it would be best if you made ‘a’ an  
instance of a class with the ‘set10’ logic in it. The sort of pass-by-  
reference pattern you’re looking for is much more typical of C.

That said, this would be, I think, the closest equivalent in Ruby:

a = 0

def set10(aArg\_name, bind)  
eval("#{aArg\_name} = 10", bind)  
end

set10(‘a’, binding)

p a

=\> 10

Cheers,

Josh

---

<div class="post-metadata">

**Author:** ![SSSS\_SSS\_SSS\_SSSSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7ba0ec/32.png) [@SSSS\_SSS\_SSS\_SSSSSS](https://www.ruby-forum.com/u/SSSS_SSS_SSS_SSSSSS)\
**Post date:** [November 26, 2008, 4:30am UTC](https://www.ruby-forum.com/t/how-to-use-a-passing-argument-returned-argument/154168/3 "2008-11-26T04:30:24Z")

</div>

On Nov 25, 8:49 pm, ±è ÁØ¿µ [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> set10(a)
> 
> p a \<— I wanna get 10 as a result.
> 
> How can I do for this one?

Well, Ruby, being a purely object-oriented language, means that  
assigning a value to a variable assigns it to a new object (instance)  
without affecting the original. So in your example, you just make the  
parameter aArg refer to a new number leaving the original untouched.

To alter the data in a parameter, you would have to call a method on  
the parameter that changes the data. For example:

* * *

def method1(string)  
string.upcase!  
end

s = “hello”  
method1(s)  
p s # displays “HELLO”

* * *

Now, some classes have immutable instances – instances that cannot  
have the data within them altered. Fixnum, Bignum, and Float (the  
basic numeric types) are all immutable. So that presents a problem.

What you could do is wrap the immutable instance in a mutable class,  
as in:

* * *

require ‘delegate’

class Mutable \< SimpleDelegator  
def initialize(value)  
@value = value  
super(@value)  
end

def reassign(new\_value)  
@value = new\_value  
**setobj** (@value)  
end  
end

def method2(number)  
number.reassign(10)  
end

n = Mutable.new(3)  
method2(n)  
p n # displays 10

n = Mutable.new(“test”)  
p n # displays “test”  
method2(n)  
p n # displays 10

* * *

I hope that’s helpful.

Eric

====

Are you interested in on-site Ruby or Ruby on Rails training  
that uses well-designed, real-world, hands-on exercises?  
[http://LearnRuby.com](http://LearnRuby.com)

---

<div class="post-metadata">

**Author:** ![SSSS\_SSS\_SSS\_SSSSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7ba0ec/32.png) [@SSSS\_SSS\_SSS\_SSSSSS](https://www.ruby-forum.com/u/SSSS_SSS_SSS_SSSSSS)\
**Post date:** [November 26, 2008, 5:43am UTC](https://www.ruby-forum.com/t/how-to-use-a-passing-argument-returned-argument/154168/4 "2008-11-26T05:43:28Z")

</div>

On 26.11.2008, at 04:19 , Joshua B. wrote:

> > 
> 
> Cheers,
> 
> Josh

I did not know this, google filled me in and this might be relevant to  
the original question:  
[http://onestepback.org/index.cgi/Tech/Ruby/RubyBindings.rdoc/style/print](http://onestepback.org/index.cgi/Tech/Ruby/RubyBindings.rdoc/style/print)

They end up with this:

def swap(aref, bref)  
aref.value, bref.value = bref.value, aref.value  
end

a = 22  
b = 33  
swap(ref{:a}, ref{:b})  
p a # =\> 33  
p b # =\> 22

Pretty neat, if that’s something you wanna do. Makes me wonder though:  
when I first saw this (5 minutes ago) I thought you might be able to do

a = 0

def set10(aArg\_name, bind=binding)  
eval(“#{aArg\_name} = 10”, bind)  
end

set10(‘a’)

p a

but that doesn’t work, in what context and when are default-values  
evaluated? my guess is that they get the same closure as the method  
body and are evaluated on call, is that correct?

einarmagnus

---

<div class="post-metadata">

**Author:** ![SSSS\_SSS\_SSS\_SSSSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7ba0ec/32.png) [@SSSS\_SSS\_SSS\_SSSSSS](https://www.ruby-forum.com/u/SSSS_SSS_SSS_SSSSSS)\
**Post date:** [November 26, 2008, 5:58am UTC](https://www.ruby-forum.com/t/how-to-use-a-passing-argument-returned-argument/154168/5 "2008-11-26T05:58:40Z")

</div>

Einar’s suggestion is very good and clear everything for me 🙂

finally, my code should be changed like --\>

class Reference  
def initialize(var\_name, vars)  
@getter = eval “lambda { #{var\_name} }”, vars  
@setter = eval “lambda { |v| #{var\_name} = v }”, vars  
end  
def value  
@getter.call  
end  
def value=(new\_value)  
@setter.call(new\_value)  
end  
end

def ref(&block)  
Reference.new(block.call, block.binding)  
end

def set10(var\_a)  
var\_a.value = 10  
end

a = 22  
set10 (ref{:a})  
p a

thanks for all 🙂

1. 
  1. 26, ì˜¤í›„ 1:38, Einar MagnÃºs Boson ìž‘ì„±:

---

<div class="post-metadata">

**Author:** ![SSSS\_SSS\_SSS\_SSSSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7ba0ec/32.png) [@SSSS\_SSS\_SSS\_SSSSSS](https://www.ruby-forum.com/u/SSSS_SSS_SSS_SSSSSS)\
**Post date:** [November 27, 2008, 3:11am UTC](https://www.ruby-forum.com/t/how-to-use-a-passing-argument-returned-argument/154168/6 "2008-11-27T03:11:46Z")

</div>

On Tue, 25 Nov 2008 23:38:02 -0500, Einar MagnÃºs Boson wrote:

> a = 22  
> b = 33  
> swap(ref{:a}, ref{:b})  
> p a # =\> 33  
> p b # =\> 22
> 
> Pretty neat, if that’s something you wanna do.

That rocks. Thanks for sharing. I thought that this kind of  
functionality  
would require syntax built into the language.

> p a
> 
> but that doesn’t work, in what context and when are default-values  
> evaluated? my guess is that they get the same closure as the method body  
> and are evaluated on call, is that correct?

Yeah. I’d love to have a Binding.of\_caller or something like that, but  
have had no such luck. (Ok. It’s an add-on in 1.8, but I’m curious about  
1.9)

---

<div class="post-metadata">

**Author:** ![SSSS\_SSS\_SSS\_SSSSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7ba0ec/32.png) [@SSSS\_SSS\_SSS\_SSSSSS](https://www.ruby-forum.com/u/SSSS_SSS_SSS_SSSSSS)\
**Post date:** [November 27, 2008, 4:58am UTC](https://www.ruby-forum.com/t/how-to-use-a-passing-argument-returned-argument/154168/7 "2008-11-27T04:58:11Z")

</div>

On 27.11.2008, at 03:06 , Lloyd L. wrote:

> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Because his question was about references, swap was just an example.

einarmagnus

---

<div class="post-metadata">

**Author:** ![SSSS\_SSS\_SSS\_SSSSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7ba0ec/32.png) [@SSSS\_SSS\_SSS\_SSSSSS](https://www.ruby-forum.com/u/SSSS_SSS_SSS_SSSSSS)\
**Post date:** [November 27, 2008, 4:11am UTC](https://www.ruby-forum.com/t/how-to-use-a-passing-argument-returned-argument/154168/8 "2008-11-27T04:11:32Z")

</div>

> a = 22  
> b = 33  
> swap(ref{:a}, ref{:b})  
> p a # =\> 33  
> p b # =\> 22

instead of swap(ref{:a}, ref{:b})

why not a, b = b, a
