# Parameter passing

**URL:** <https://www.ruby-forum.com/t/parameter-passing/194008>\
**Category:** Ruby\
**Created:** [August 27, 2010, 9:42pm UTC](https://www.ruby-forum.com/t/parameter-passing/194008 "2010-08-27T21:42:26Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![trapper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/2acd7d/32.png) [@trapper](https://www.ruby-forum.com/u/trapper)\
**Post date:** [August 27, 2010, 9:42pm UTC](https://www.ruby-forum.com/t/parameter-passing/194008/1 "2010-08-27T21:42:26Z")

</div>

Is it possible, to pass local variables by refrerence to method?

I would like to do something like that:

def meth(dest)  
dest = 0 if some\_condition  
end

…

local\_var = 1

# some\_condition evaluates to true

meth(local\_var)

local\_var == 0 # should evaluate to true

---

<div class="post-metadata">

**Author:** ![trapper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/2acd7d/32.png) [@trapper](https://www.ruby-forum.com/u/trapper)\
**Post date:** [August 27, 2010, 11:03pm UTC](https://www.ruby-forum.com/t/parameter-passing/194008/2 "2010-08-27T23:03:51Z")

</div>

On Fri, Aug 27, 2010 at 12:42 PM, Fritz T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> local\_var = 1
> 
> # some\_condition evaluates to true
> 
> meth(local\_var)
> 
> local\_var == 0 Â Â Â Â Â Â # should evaluate to true

There is no way to do exactly that in Ruby. If you pass the name of  
the local variable as a string, and pass the binding, you can  
accomplish something very similar, e.g.:

def meth(target, context)  
result = context.eval target  
context.eval “#{target} = 0”  
result  
end

local\_var = 1  
meth(“local\_var”,binding) # =\> 1  
local\_var == 0 # =\> true

* * *

But this is ugly in many ways. There are better ways of doing  
out-of-band (e.g., other than the return value) communications back to  
the caller from a method, like taking a callback function as an  
argument and passing data to the callback function. You’ll probably  
get better feedback on the best way to achieve what you want in Ruby  
if you are more specific as to the real goal.

---

<div class="post-metadata">

**Author:** ![trapper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/2acd7d/32.png) [@trapper](https://www.ruby-forum.com/u/trapper)\
**Post date:** [August 27, 2010, 11:32pm UTC](https://www.ruby-forum.com/t/parameter-passing/194008/3 "2010-08-27T23:32:11Z")

</div>

Thanks for your reply. I’ll think about another way to solve my problem.

One great appeal:

PLEASE STOP POSTING MY EMAIL ADDRESS HERE IN THE FORUM. I DONT WANT  
TO GET  
FLOODED BY SPAMMERS!

---

<div class="post-metadata">

**Author:** ![trapper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/2acd7d/32.png) [@trapper](https://www.ruby-forum.com/u/trapper)\
**Post date:** [August 28, 2010, 3:03am UTC](https://www.ruby-forum.com/t/parameter-passing/194008/4 "2010-08-28T03:03:16Z")

</div>

On Fri, 27 Aug 2010 16:32:14 -0500, Fritz T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote in [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> One great appeal:
> 
> PLEASE STOP POSTING MY EMAIL ADDRESS HERE IN THE FORUM. I DONT WANT  
> TO GET FLOODED BY SPAMMERS!

This forum is gatewayed to the ruby-talk mailing list and  
comp.lang.ruby newsgroup. Your email address is being used as part of  
a standard attribution line, which is automatically generated by email  
and news clients. People are not going to remember to make an  
exception for you, so you should either post with a different address  
(e.g. a gmail address) or make sure that you use good spam filters.

---

<div class="post-metadata">

**Author:** ![trapper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/2acd7d/32.png) [@trapper](https://www.ruby-forum.com/u/trapper)\
**Post date:** [August 28, 2010, 11:40am UTC](https://www.ruby-forum.com/t/parameter-passing/194008/5 "2010-08-28T11:40:36Z")

</div>

On 27.08.2010 23:32, Fritz T. wrote:

> Thanks for your reply. I’ll think about another way to solve my problem.

Interestingly I cannot remember having felt the need for C++ like call  
by reference (which is what you probably have in mind). I suspect the  
reason is in the way I try to design OO applications. With a proper  
concept of classes with local state (which can then be changed) your  
problem might be solved differently. If you provide a bit more detail  
about what it is that you are trying to achieve then we might be able to  
come up with other suggestions.

> One great appeal:
> 
> ```
> PLEASE STOP POSTING MY EMAIL ADDRESS HERE IN THE FORUM. I DONT WANT
> 
> ```
> 
> TO GET  
> FLOODED BY SPAMMERS!

Please do not shout. Nowadays everybody must be aware that every email  
address you use in public is highly likely to be targeted by spam  
eventually. Unfortunately there is no way to undo the leakage of an  
email address so you better use a spam friendly address upfront.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![trapper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/2acd7d/32.png) [@trapper](https://www.ruby-forum.com/u/trapper)\
**Post date:** [August 28, 2010, 12:23pm UTC](https://www.ruby-forum.com/t/parameter-passing/194008/6 "2010-08-28T12:23:33Z")

</div>

Robert K. wrote:

> Interestingly I cannot remember having felt the need for C++ like call  
> by reference (which is what you probably have in mind).

Exactly. I have a lot of experiences with C/C++ und ruby is a very  
different and fascinating philosophy.

I solved my problem by passing an object.

Regarding posting email addresses: I usually use junk addresses for  
purposes like this and was quite surprised, that this forum does nothing  
to hide private email addresses. I do not remember, that I saw any hint,  
that my address will be published.

---

<div class="post-metadata">

**Author:** ![trapper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/2acd7d/32.png) [@trapper](https://www.ruby-forum.com/u/trapper)\
**Post date:** [August 30, 2010, 7:40pm UTC](https://www.ruby-forum.com/t/parameter-passing/194008/7 "2010-08-30T19:40:09Z")

</div>

> local\_var = 1
> 
> # some\_condition evaluates to true
> 
> meth(local\_var)
> 
> local\_var == 0 # should evaluate to true

You could pass it as an array  
local\_var = [1]  
meth(local\_var) # does local\_var[0] = 1

---

<div class="post-metadata">

**Author:** ![trapper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/2acd7d/32.png) [@trapper](https://www.ruby-forum.com/u/trapper)\
**Post date:** [August 28, 2010, 12:28pm UTC](https://www.ruby-forum.com/t/parameter-passing/194008/8 "2010-08-28T12:28:26Z")

</div>

Interestingly, the webmaster also does’nt like to publish his address to  
spam bots:

```
E-Mail: webmaster (at) ruby-forum (dot) com.
```
