binding_of_caller

  • Thread starter Wolfgang Nádasi-Donner
  • Start date
W

Wolfgang Nádasi-Donner

Moin, moin!

I didn't find a topic which tells me, if a method "binding_of_caller" is
planned for Ruby's future. There was a method available via a library,
but it doesn't work anymore, because it was based on a bug (a nice bug,
indeed).

I would like to have a method like this, because it avoids unnecessary
parameters under some circumstances.

The actual example was a the answer to a variable reference, which was
needed by some member of the german Ruby forum.
module Kernel
def ref(v, bnd)
v = v.to_s
eval('lambda{|*p|p.length==0 ? ' + v + ' : ' + v + '=p[0]}', bnd)
end
end

class Proc
def -@
self[]
end
def <<(v)
self[v]
end
end

a = 42
b = 84

x = ref :a, binding # unfortunately necessary
y = ref :b, binding # unfortunately necessary

puts a # => 42
puts -x # => 42
puts b # => 84
puts -y # => 84

puts x << 55 # => 55
puts a # => 55
puts -x # => 55

z = ref :y, binding # unfortunately necessary

puts b # => 84
puts --z # => 84

puts -z << 75 # => 75
puts -y # => 75
puts --z # => 75
puts b # => 75
Wolfgang Nádasi-Donner
 
G

gabriele renzi

Moin, moin!

I didn't find a topic which tells me, if a method "binding_of_caller" is
planned for Ruby's future. There was a method available via a library,
but it doesn't work anymore, because it was based on a bug (a nice bug,
indeed).

I can't answet the question, but I believe you can get that functionality
via the gem ruby-debug, now.
I would like to have a method like this, because it avoids unnecessary
parameters under some circumstances.

FWIW, I'd love to have that too. Or better, having Frame objects returned
by Kernel#caller.
 
W

Wolfgang Nádasi-Donner

gabriele said:
I can't answet the question, but I believe you can get that
functionality
via the gem ruby-debug, now.

Thank you for this quick response. I've installed it now, and must find
a way to make it work ;-)

But my general question is still open, because it should be usable
without the debugger.

I would like to have a method "Kernel#binding_of_caller" (or with a
different name), which returns the Binding object of the caller's
environment or "nil" if it is called on top leven environment.

If a method with this features doesn't exist somewhere deep inside Ruby
now, I tend to write a RCR, but will ask before if it is wanted by one
person (=WoNáDo) only.

Wolfgang Nádasi-Donner
 
W

Wolfgang Nádasi-Donner

Wolfgang said:
If a method with this features doesn't exist somewhere deep inside Ruby
now, I tend to write a RCR, but will ask before if it is wanted by one
person (=WoNáDo) only.

I think a RCR is not necessary. I am surprised a bit that only one other
person is interested in the topic.

Wolfgang Nádasi-Donner
 
T

Trans

I think a RCR is not necessary. I am surprised a bit that only one other
person is interested in the topic.

It's been talked about LOTS, and generally favored it seems to me. But
the "masters" don't seem to agree. They are right of course that it is
"dangerous" --it can create a strong coupling between a method and
it's caller, however, used intelligently it can be quite useful in
certain meta-programming cases.

I've always thought of Ruby as the language that makes you very aware
of your left foot --so you don't shoot it off. But in some cases, like
this one, it's still socking up a couple toes.

T=2E
 

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,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top