From: dblack@... Date: 2002-08-09T01:55:09+09:00 Subject: Enumerable#any? (was: Re: Coding challenge (on Ruby Garden) [long]) Hi -- On Thu, 8 Aug 2002, Christoph wrote: > > "Bill Kelly" wrote in message > news:002901c23e70$402b3d30$fa7b1b42@musicbox... > > Hi, > > > > > Hm, maybe you want to benchmark my solution, Chr0002, as well? > > > > Whoops, I don't seem to have Array#any? and Array#all?. :) New > > methods in 1.7 I guess? > > Yup, they are a generalization of && and || to Enumerables. > > module Enumerable > def all? > each {|e| return false unless yield e } > return true > end > def any? > each {|e| return true if yield e } > return false > end > end At first glance I thought that #any? didn't add anything to just using the Boolean value of the result of #detect... until I tried this: ar = [1,2,3,nil,5] puts "nil found by any?" if ar.any? {|e| e == nil } puts "nil found by detect" if ar.detect {|e| e == nil} :-) David -- David Alan Black home: dblack@candle.superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav