From: Robert Klemme Date: 2003-05-26T21:59:32+09:00 Subject: Re: exiting of a block "Emmanuel Touzery" schrieb im Newsbeitrag news:002d01c3236d$64226910$1600a8c0@emmanuels... > > "Robert Klemme" wrote in message > news:bashj0$33hl9$3@ID-52924.news.dfncis.de... > > > > # now find the current program for this > > > # channel. > > > programsForChannel.find { |program| > > > result = false > > > if program.time <= showTime > > > if program.endTime > > > # there is a endtime. > > > # are we now before this end? > > > if program.endTime > showTime > > > result = true > > > end > > > else > > > # program has no registered > > > # end. > > > result = true > > > end > > > end > > > result > > > } > > > > > > any help appreciated! > > > > In your case you can use "return" even with older versions of ruby if you > > use a method: > > > > def find > > [1, 2, 3].find { |e| > > return true if e == 2 > > } > > > > false > > end > > > > Are you aware of the standard methods Enumerable#include?, > > Enumerable#find, Enumerable#detect, Enumerable#select, Enumerable#find_all > > and Enumerable#grep? These should provide the functionality you need. > > the code you give returns returns true or false, i want the item that is > found; Well, your example (the simple one) returned 'true' or 'false'. This is an easy change: def find [1, 2, 3].find { |e| return e if e == 2 } nil end But that's exactly what Enumerable#detect and Enumerable#find do. > i actually want to use Enumerable#find, as I do, but with a complex > condition. the problem is that in my condition i have several places to say > "yes, this is the item i want" or "no, it's not the item i want". in a > function i would use return, but in that case, that pops me out of the whole > function, not only of the block. If that condition is so complex, i'd either a) include it in the class of the instances in the collection (I guess it's called "Program" or similar) or b) define a function for it That way you get better documentation, reusability and get rid of the return problem. However, variant a) seems most natural (i.e. OO) to me. robert