From: Yukihiro Matsumoto Date: 2005-03-16T10:46:19+09:00 Subject: Re: eval/binding question Hi, In message "Re: eval/binding question" on Wed, 16 Mar 2005 02:45:41 +0900, Stefan Kaes writes: |In the real environment, the names of the locally assigned variables can |not be determined in advance, of course. I'm sorry to say you can't pre-compile eRB allowing local variable set not determined in advance. I know allowing this will make your program 25% faster, but this also would make any other programs slower. If you have limited set of local variables, you can do something like: require 'erb' class Test def initialize(path, *lv) @erb = ERB.new(File.read(path), nil, '%') @locals = {} evalstr = "def run_erb\n" + lv.collect{|k| "#{k} = @locals[\"#{k}\"]\n"}.join + "#{@erb.src}\n_erbout\nend\n" print evalstr, "\n\n" eval evalstr, nil, path, 0 end def values(hash) @locals.update(hash) hash.each {|k,v| self.instance_variable_set("@#{k}", v) } end end t = Test.new("/tmp/test.rhtml", 'mumu') t.values("mumu" => "HELLO") print t.run_erb matz.