From: Lloyd Zusman Date: 2005-03-03T09:33:00+09:00 Subject: Re: Erb: terminating a script? Jamis Buck writes: > On Mar 2, 2005, at 4:02 PM, Lloyd Zusman wrote: > >> [ ... ] >> res.body = evaluate(ERB.new('<% lambda { %>' + >> data + >> '<% }.call %>'), >> req, res) >> >> In this case, I can exit early by using either '<% break %>' or >> '<% return %>'. I like this one better, and this is what I have >> just now implemented in my application. > > Excellent work, Lloyd. Thanks for sharing! Well, I'm just glad to have found a solution, and I hope that it's helpful to others. >> Because of this, it seems to me that it would be beneficial if Erb were >> to always put a lambda-call block around the text that gets evaluated. >> >> I'm in favor of this enhancement going into a subsequent release, and >> I'll soon submit a patch. >> >> Thoughts or opinions? > > I'd vote for it. :) OK. Here it is. Unless or until this becomes a bona-fide Erb enhancement, just put this code snippet in your application, somewhere after the "require 'erb'" statement: class ERB def result(b=TOPLEVEL_BINDING) # Enclosing the 'eval' in a 'lambda-call' block allows it # to be cut short by means of a 'break' or 'return'. if @safe_level th = Thread.start { $SAFE = @safe_level lambda { eval(@src, b, (@filename || '(erb)'), 1) }.call } return th.value else return lambda { eval(@src, b, (@filename || '(erb)'), 1) }.call end end end -- Lloyd Zusman ljz@asfast.com God bless you.