From: Brian Candler Date: 2009-04-15T21:19:48+09:00 Subject: Re: Proc.call with custom bindings Robert Klemme wrote: >> The reason I am asking, is that I am thinking about improving >> performance of template engines that generate Ruby code upon parsing >> template, and then call eval(@precompiledsrc) upon each render of >> template. > > Why would anyone want to do that? I think he's saying that he had found a template engine which stored the string and eval'd it each time it needed to be rendered; he wanted to improve on that situation. I think vanilla ERB falls into that category. irb(main):001:0> require 'erb' => true irb(main):002:0> e = ERB.new("<%= foo %>") => # irb(main):003:0> lambda { foo = 123; e.result(binding) }.call => "123" irb(main):004:0> lambda { foo = 456; e.result(binding) }.call => "456" Source of ERB#result: def result(b=TOPLEVEL_BINDING) if @safe_level th = Thread.start { $SAFE = @safe_level eval(@src, b, (@filename || '(erb)'), 1) } return th.value else return eval(@src, b, (@filename || '(erb)'), 1) end end -- Posted via http://www.ruby-forum.com/.