From: Steve V Date: 2005-04-19T11:56:36+09:00 Subject: Re: Store erb block and alter scope of erb block eval? > 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

" > Is there something special erb does when you capture a block from an rhtml page? I tried passing my own variable values to the block within my class(separate from the controller), and the values are ignored. I think the heart of the problem is that you're calling ERB.new on a regular string. I need to capture this information from an rhtml page. The only way I have found to do that is by using a "do" block. r.content do%> Test <%=@sometext%> <%end%> This gives me a block, and not a string. Is there a way to turn the contents of the returned proc as a string? to_s just gives me the memory location(?) of the proc object. The other question is why aren't my variables used for the block call when I'm in the class? Does it have something to do with the existing scope of the proc object? Can I change the scope? The only place I can get that @sometext value to be used from is within the controller. I have tried defining @sometext within my separate class, and passing that into the call, but nothing happens. def content(&block) @sometext = "sometext" @data = block.call(@sometext) end def render() return @data end The above will just render "Test ", and not use the specified value for @sometext. Steve