From: Gavin Kistner Date: 2005-04-19T10:25:12+09:00 Subject: Re: Store erb block and alter scope of erb block eval? On Apr 18, 2005, at 3:54 PM, Steve V wrote: > 1) How can I store an erb block captured using do for later use? > Regular > variable assignment doesn't appear to work. Works for me: class Foo def set_block( &block ); @meth = block; end def call_block( *args ); @meth.call( *args ); end end f = Foo.new f.set_block { |x,y| p x,y } f.call_block 3,4 #=>3 #=>4 > 2) When evaluating 'block', how can I get it to use the variables > present > within the scope of the calling function, or at least within the > calling > class? require 'erb' class Foo def rhtml=( str ) @rhtml = ERB.new( str ) end def run1 localvar = 'run1' @rhtml.result( binding ) end def run2 localvar = 'run2' @rhtml.result( binding ) end end f = Foo.new f.rhtml = '

<%= localvar %>

' p f.run1 #=> "

run1

" p f.run2 #=> "

run2

"