From: andrew Date: 2007-03-19T05:20:05+09:00 Subject: Re: accessing methods from the blocks caller I'm experiencing the issue using Rails. I wanted to leave mention of Rails out of my post as I figured it was simply a Ruby issue, but perhaps not. class MyController < ApplicationController def some_action render :update do |page| render_to_string # getting an error that render_to_string is undefined end end end I fix this by doing this... def some_action str = render_to_string render :update to |page| # use str in the block end end So for anybody with a bit of rails knowledge, do you know what's going on? I can ask this in a rails group now if you think it's off topic. Thanks again, Andrew "eden li" wrote in message news:1174204947.155558.226150@d57g2000hsg.googlegroups.com... > 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. > >