Parameter passing

F

Fritz Trapper

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
 
C

Christopher Dicely

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

I would like to do something like that:

def meth(dest)
=C2=A0dest =3D 0 if some_condition
end

...

local_var =3D 1
# some_condition evaluates to true
meth(local_var)

local_var =3D=3D 0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0# should eval=
uate 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 =3D context.eval target
context.eval "#{target} =3D 0"
result
end

local_var =3D 1
meth("local_var",binding) # =3D> 1
local_var =3D=3D 0 # =3D> 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.
 
F

Fritz Trapper

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!
 
C

Charles Calvert

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.
 
R

Robert Klemme

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
 
F

Fritz Trapper

Robert said:
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.
 
F

Fritz Trapper

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

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

Roger Pack

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,146
Messages
2,570,831
Members
47,374
Latest member
anuragag27

Latest Threads

Top