Setting a 'reference' inside the method body

P

Peter Szinek

Hey guys,

Can Ruby do this in some way:

def foo(bar)
#DO SOMETHING with bar here resulting in baz being set to {}
end

baz = {:fluff => :eek:rk}
foo(baz)
#baz should be {} now

I'd like to use it in a code which calls a callback, (the foo method in
this case) but the callback is called from multiple places, and only
inside foo() we know whether we want to empty the array or not... it
would be complicated to return something from foo() with the current
architecture (based on which we could empty the hash or not).


My Ruby version is JRuby 1.6 if that makes any difference.

Cheers,
Peter
 
X

Xavier Noria

def foo(bar)
=C2=A0#DO SOMETHING with bar here resulting in baz being set to {}
end

baz =3D {:fluff =3D> :eek:rk}
foo(baz)
#baz should be {} now

I'd like to use it in a code which calls a callback, (the foo method in
this case) but the callback is called from multiple places, and only
inside foo() we know whether we want to empty the array or not... it
would be complicated to return something from foo() with the current
architecture (based on which we could empty the hash or not).

You cannot change the reference stored in the caller's baz, because
Ruby has pass by value semantics. But hashes are mutable, in
particular

baz.clear

wipes the hash, so the caller will see it empty as well.
 
B

Brian Candler

Xavier Noria wrote in post #995127:
You cannot change the reference stored in the caller's baz, because
Ruby has pass by value semantics. But hashes are mutable, in
particular

baz.clear

wipes the hash, so the caller will see it empty as well.

(bar.clear that is). Using bar.replace({}) is another option. Both of
these mutate the Hash that was passed in.

In Ruby:

- all values are object references
- all method calls are pass-by-value
- a local variable is *not* itself an object
- you cannot take a "reference" to a local variable

Almost certainly you want to mutate the Hash being passed in, not
attempt to modify the caller's local variable which holds the reference
to that Hash.

That said, there is one nasty way to achieve specifically what you
asked:

def bar(b)
eval "baz='whoopee'", b
end

baz = {:fluff => :eek:rk}
bar(binding)
p baz

It involves either passing a Binding (as shown), or passing a block,
which also carries a Binding, and then evaluating code in the context of
that Binding. Unless you're writing a debugger or something like that,
you almost certainly don't want to do this.
 
C

Charles Oliver Nutter

It involves either passing a Binding (as shown), or passing a block,
which also carries a Binding, and then evaluating code in the context of
that Binding. Unless you're writing a debugger or something like that,
you almost certainly don't want to do this.

FWIW, I always discourage people from using blocks as bindings,
because it defeats a number of potential optimizations. Specifically,
if blocks can be used as bindings:

* All variables in the containing scope must be made accessible
in-memory somewhere, so you can't eliminate or optimize them away.
* All bodies containing blocks must have a full Ruby execution
environment (frame, scope, etc), so you can't do frame elimination
when blocks are present.
* There's also potential for really nasty threading effects, if the
block binding is passed to another thread that mutates variables in
the containing scope.

Without this feature, I know for a fact that JRuby could optimize
blocks a lot more than it does right now, potentially making them as
fast or faster than methods. We could also reduce the overhead of
methods that contain closures, since right now we have to deoptimize
to handle the block-as-binding case.

I also believe it's a code security/visibility issue, since any method
you pass a block to can read all your local variable state. For
example...

def secure_login(password)
with_transaction do
.... do login ....
end
end

...and in some transaction library that has been compromised:

def with_transaction(&block)
stolen_password = eval "defined?(password) ? password : nil"
email_password_to_hacker(stolen_password) if stolen_password
... normal transaction logic...
end

With the potential for any code to monkey-patch any other code, this
seems like a very real threat. I don't believe library code should be
able to access the calling method's state unless I make that state
available to it.

It definitely works, but I have lobbied (and continue to lobby) to remove it.

- Charlie
 
R

Robert Dober

I guess you meant - and I corrected this for newbies,

=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0eval( "defined?(password) ?
password : nil", block.binding)

but for the rest, although not of much help to you, I feel your pain.

Cheers
Robert
 

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

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top