From: eden li Date: 2007-03-18T17:02:43+09:00 Subject: Re: accessing methods from the blocks caller In the three cases I could think of, "method1" was always available to the block. AFAIK, the block you pass to #accept_block no matter where it is will always be bound to the lexical context you call it in, meaning that 'method1' will always be available unless you do something special when you call the block. Maybe it'll help if you list the source for #accept_block and the exact error you're getting. # case 1, 'accept_block' defined in a super class class S def foo; yield; end end # case 2, 'accept_block' defined in an included module module M def bar; yield; end end # case 3, 'accept_block' defined in some other class class D def baz; yield; end end class C < S include M def m1; 'hi'; end def test_foo; foo { m1 }; end def test_bar; bar { m1 }; end def test_baz; D.new.baz { m1 }; end end >> c = C.new => # >> c.test_foo => "hi" >> c.test_bar => "hi" >> c.test_baz => "hi" On Mar 18, 1:30 pm, "andrew" wrote: > Here's my question... if I have a method that accepts a block and I want to > call a method of the class I'm in, in that block, how do I do it? See the > comment below in the example. Should make that sentence a bit clearer. :) > > 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.