Binding_of_caller

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.

Code >>>>>
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

EoC >>>>>

Wolfgang Nádasi-Donner

On Tue, 02 Oct 2007 18:48:25 +0900,
Wolfgang Nádasi-Donner wrote:

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.

gabriele renzi wrote:

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 :wink:

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

Wolfgang Nádasi-Donner wrote:

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

On Oct 13, 5:38 am, “Wolfgang Nádasi-Donner” [email protected]
wrote:

Wolfgang Nádasi-Donner wrote:

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.

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.