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