From: Stefan Kaes Date: 2005-03-16T02:45:41+09:00 Subject: Re: eval/binding question --------------050504010907080609010900 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit code attached. In the real environment, the names of the locally assigned variables can not be determined in advance, of course. --------------050504010907080609010900 Content-Type: text/plain; name="evalbinding.rb" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="evalbinding.rb" require 'erb' class ERBrunner #all compiled template functions go here def get_binding binding end end Erb_runner = ERBrunner.new class Test def initialize @erb = ERB.new(File.read("test.rhtml"), nil, '%') evalstr = "def run_erb\n" + "bnd = binding\n" + '@locals.each{|k,v| eval "#{k} = @locals[\"#{k}\"]", bnd }' + "\n#{@erb.src}\n_erbout\nend\n" print evalstr, "\n\n" eval evalstr, Erb_runner.get_binding end def runit Erb_runner.instance_variable_set "@mumu", "HALLO" Erb_runner.instance_variable_set "@locals", {'mumu'=>"HALLO"} bnd = Erb_runner.get_binding eval "Erb_runner.run_erb", bnd end end t = Test.new print t.runit --------------050504010907080609010900 Content-Type: text/plain; name="test.rhtml" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="test.rhtml" locals: <%= @locals.inspect %> locals['mumu']: <%= @locals['mumu'] %> mumu: <%= mumu rescue "not defined" %> @mumu: <%= @mumu %> --------------050504010907080609010900--