From: Joel VanderWerf Date: 2009-06-03T07:10:38+09:00 Subject: Re: return in iterator in lambda Benjamin Ter kuile wrote: > I want to create a conditional mathematical function as a lambda > structure. Therefore it may be the case that inside an iterative loop, > the result of the function is known and should be returned. By > implementing this it causes trouble. The test code I created looks like: > > > func = lambda do > for z in [1,2,3] > return 2 > end > "something" > end > puts func.call > puts "after call" > > This results in a LocalJumpError (Ruby 1.8.6 and 1.8.7). In ruby 1.9 it > works as expected. Is there a proper way of creating a function like > this in ruby < 1.9? Enumerable#find may be more appropriate than #each. This avoids the issue of how to break out of nested blocks. func = lambda do |a| a.find do |z| z if z >= 1.5 end or "not found" end puts func.call([1,2,3]) puts func.call([0,0.2,0.4]) -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407