From: Jeff de Vries Date: 2002-11-28T13:40:46+09:00 Subject: Re: Array#and? and Array#or? methods --------------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
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--