From: Farrel Lifson Date: 2006-10-20T23:37:08+09:00 Subject: Re: break from block On 20/10/06, Robert Klemme wrote: > On 20.10.2006 15:45, Farrel Lifson wrote: > > I've just run into the following problem. Doing this: > > block = lambda{|i| i > 5 ? break : nil} > > n.times &block > > raises a LocalJumpError, I assume because break is not valid from > > within a Proc object. Is there any way to get around this? > > The direct form works. > > irb(main):008:0> 10.times {|i| p i; break if i > 5} > 0 > 1 > 2 > 3 > 4 > 5 > 6 > => nil > > Hm, at the moment I do not have an idea. What do you need that for? > Maybe there is a different solution. For example: > > irb(main):009:0> crit = lambda {|i| i > 5} > => # > irb(main):010:0> 10.times {|i| p i; break if crit[i]} > 0 > 1 > 2 > 3 > 4 > 5 > 6 > => nil > > Cheers > > robert I'm just trying to optimise some code and I was wondering if doing block = lambda{|i| #processing; if i > 5; break} n.times &block would be any faster than doing it directly. Farrel