From: "Mauricio Fernández" Date: 2002-11-29T02:14:59+09:00 Subject: Re: Array#and? and Array#or? methods On Thu, Nov 28, 2002 at 01:40:46PM +0900, Jeff de Vries wrote: > > Thanks for pointing out Enumerable#detect. I just started looking at > Ruby this week, and didn't think to look at methods other than in Array! > > detect seems to work easily for the "or?" case: > > def even?(n) > (n&1) == 0 > end > > [1,2,3].detect {|x| even? x} ==> 2 > > (except that it returns the element found, rather than true or false, > which causes a problem if the found element is nil!) I didn't know detect until I read about it in the reply to your first message. It was quite a surprise (the magic word to trigger attention in ruby-talk ;-) to find that it was an alias to Enumerable#find (or rather the opposite), as I would have expected it to return 'true' or 'false'. > For the "and?" case it is a bit awkward, though, because you have to "de > Morgan" the result and your test by negating them: > > ! [2,4,6].detect {|x| ! even? x} ==> true > > It sounds like Ruby 1.7 has what I want (except named all? and any?, but > that is okay ;^) > > Thanks for the comments! > > [BTW, I had to send/edit this reply twice to get it past SpamAssassin, > who kept rejecting it as spam!] Please tell Shugo Maeda ! I'm sure he'll be glad to be taught how to write SPAM-style :-) http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/54448 -- _ _ | |__ __ _| |_ ___ _ __ ___ __ _ _ __ | '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \ | |_) | (_| | |_\__ \ | | | | | (_| | | | | |_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_| Running Debian GNU/Linux Sid (unstable) batsman dot geo at yahoo dot com We come to bury DOS, not to praise it. -- Paul Vojta, vojta@math.berkeley.edu