From: "Benjamin J. Tilly" Date: 2001-06-19T03:44:19+09:00 Subject: [ruby-talk:16599] RE: RCR: Enumerable: every() and none() >===== Original Message From Hugh Sasse Staff Elec Eng ===== >I've been off the list for a while but I'm dipping back in to suggest: > >Enumerable should have functions every and none to complement find_all >and reject. I think it is likely that people will want to know if all >the things in the enumObj match or all don't match the conditions >specified by block. So: Personally I would use detect (or equivalently find) with a not inserted either before the detect (for none) or before the condition in the block (for every). I am comfortable putting that logic inline, and when possible I like having a relatively small circle of ideas that play well together rather than a lot of slightly different permutations on the same thing. YMMV. It will definitely vary if your array potentially contains nil or false and you are interested in detecting that. The overloading of the return of detect/find makes this problematic, however the situation does not appear to arise as often as you might fear. (However it is something to be aware of.) >def every(&block) > find_all(&block) == to_a() >end > >def none(&block) > reject(&block) == to_a() >end [...] >that EVERY *params is numeric, or that EVERY *params is a trapezoid.... >I submit that this is generalized enough and useful enough to merit >inclusion in ruby, hence my proposing this RCR. There may be a more efficient >way to implement this than with the above code, so that is intended only >as a description of functionality. I would definitely recommend breaking out on the first element found that didn't fit the assertion. Failing, that the following will be more efficient, particularly if the collection has complex entries: def every(&block) reject(&block) == [] end def none(&block) find_all(&block) == [] end Cheers, Ben