--------------040902040700050806010301
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit


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!)

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!]


dblack / candle.superlink.net wrote:

>Hi --
>
>On Thu, 28 Nov 2002, Jeff de Vries wrote:
>
>  
>
>>Is there some reason the following methods on Array aren't included as
>>standard?
>>
>>class Array
>>  def and?
>>    # return true if the provided block returns true for every element
>>    each {|x| return false unless yield x} if block_given?
>>    return true
>>  end
>>  def or?
>>    # return true if the provided block returns true for at least one
>>element
>>    each {|x| return true if yield x} if block_given?
>>    return false
>>  end
>>end
>>
>>I find these to be very useful, and was suprised to not see them.  Is
>>there another way to get the same effect (including short circuit of
>>evaluation as soon as a condition fails)?
>>    
>>
>
>Yes, you can use detect:
>
>  a  1,2,3,4]
>  a.detect {|e| e 2}
>
>and just use the logic however you need it:
>
>  puts "failure" unless a.detect {|e| e 3}
>
>etc.  (For simple cases like this you can also use include? )
>
>
>David
>
>  
>


--------------040902040700050806010301
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta http-equiv
ontent-Type" contentext/html;charsetO-8859-1"> <title></title> </head> <body> <div classoz-text-html" lang-western"> Thanks for pointing out Enumerable#detect. &nbsp;I just started looking at Ruby this week, and didn't think to look at methods other than in Array!<br> <br> detect seems to work easily for the "or?" case:<br> <tt><br> &nbsp; def even?(n)<br> &nbsp; &nbsp; (n&amp;1) 0<br> &nbsp; end<br> <br> &nbsp; [1,2,3].detect {|x| even? x} &nbsp;gt; 2<br> <br> </tt>(except that it returns the element found, rather than true or false, which causes a problem if the found element is nil!)<br> <br> For the "and?" case it is a bit awkward, though, because you have to "de Morgan" the result and your test by negating them:<br> <br> <tt>&nbsp; ! [2,4,6].detect {|x| ! even? x} &nbsp;gt; true<br> <br> </tt>It sounds like Ruby 1.7 has what I want (except named all? and any?, but that is okay ;^)<br> <br> Thanks for the comments!<br> <br> [BTW, I had to send/edit this reply twice to get it past SpamAssassin, who kept rejecting it as spam!]<br> <br> <br> <a classoz-txt-link-abbreviated" hrefailto:dblack / candle.superlink.net">dblack / candle.superlink.net</a> wrote:<br> <blockquote type
ite" citeidPine.LNX.4.44.0211271221590.5760-100000 / candle.superlink.net"> <pre wrap>Hi -- On Thu, 28 Nov 2002, Jeff de Vries wrote: </pre> <blockquote type
ite"> <pre wrap>Is there some reason the following methods on Array aren't included as standard? class Array def and? # return true if the provided block returns true for every element each {|x| return false unless yield x} if block_given? return true end def or? # return true if the provided block returns true for at least one element each {|x| return true if yield x} if block_given? return false end end I find these to be very useful, and was suprised to not see them. Is there another way to get the same effect (including short circuit of evaluation as soon as a condition fails)? </pre> </blockquote> <pre wrap><!----> Yes, you can use detect: a 1,2,3,4] a.detect {|e| e 2} and just use the logic however you need it: puts "failure" unless a.detect {|e| e 3} etc. (For simple cases like this you can also use include? ) David </pre> </blockquote> </div> <br> </body> </html> --------------040902040700050806010301--