From: Brian Candler Date: 2007-03-18T19:30:30+09:00 Subject: Re: accessing methods from the blocks caller On Sun, Mar 18, 2007 at 02:30:04PM +0900, andrew wrote: > class Test > def method1 > end > > def method2 > accept_block do # accept_block is not part of the Test class > ... > method1 # I want to call method1 from the class I'm in, but I'm getting undefined method for method1 > ... > end > end > end > > I understand why I would be getting this error, but I don't know how to fix > it. Is there a way to access the caller in the block in order to access the > method of it? Kernel.caller is the closest I found, but it's not what I > want. As people have said, this *should* work. Post a complete example where it fails, and the error message. Having said that, if there is a need to, the current object is called 'self' and can be passed around explicitly if you wish: class Test def accept_block(some_object) yield some_object end def method1 puts "whoo!" end def method2 accept_block(self) do |s| # ... s.method1 # ... end end end Test.new.method2 # prints "whoo!" ---- But this particular example works without: class Test def accept_block yield end def method1 puts "whoo!" end def method2 accept_block do # ... method1 # ... end end end Test.new.method2 # prints "whoo!" Regards, Brian.