Binding of a Method object?

Hi,

some methods can be defined like so:

class Test
  y = :hello
  define_method(:hello) { y }
end

Can I get access to the bindings of the Test method object so i can
access and modify the ‘y’ variable?

t = Test.new.method(:hello).send(:binding).eval('y')

and so on does not appear to work.

Thanks,

John

On 11/08/2010 06:58 PM, John M. wrote:

access and modify the ‘y’ variable?

 t = Test.new.method(:hello).send(:binding).eval('y')

and so on does not appear to work.

Can’t think of an easy way to do that, but does this help?

class Test
def self.define_method(name, &bl)
(@procs||={})[name] = bl
super
end

 def self.eval_in_class_scope_around_method method, str
   eval str, @procs[method]
 end

 y = :hello_world
 define_method(:hello) { y }

end

t = Test.new.method(:hello).send(:binding).eval(‘y’)

p Test.eval_in_class_scope_around_method :hello, “y”

You can of course put the two class methods in a module and extend Test
with it.

There is also binding_of_caller, but at least the above is fairly
elementary ruby.

Hehe, thanks. Not quite what i had in mind, but interesting nonetheless
:slight_smile: cheers