Array#and? and Array#or? methods?

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

Hi –

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

···

On Thu, 28 Nov 2002, Jeff de Vries wrote:


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

Jeff de Vries jdevries@pfrog.com writes:

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

Ruby 1.7 has Enumerable#all? and Enumerable#any? .

···


eban

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

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 :wink: 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 shugo@ruby-lang.org!
I’m sure he’ll be glad to be taught how to write SPAM-style :slight_smile:
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/54448

···

On Thu, Nov 28, 2002 at 01:40:46PM +0900, Jeff de Vries wrote:


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
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