Parameter passing

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

On Fri, Aug 27, 2010 at 12:42 PM, Fritz T. [email protected]
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.

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!

On Fri, 27 Aug 2010 16:32:14 -0500, Fritz T. [email protected]
wrote in [email protected]:

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.

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

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.

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

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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs