Extra Array methods?

I have been using a little extension of Array that I wrote for awhile:

class Array
def and?() each{|e| return false unless yield e }; true end
def or?() each{|e| return true if yield e }; false end
end

[1,2,3].and?{|e| e>0}
=> true
[1,2,3].and?{|e| e>1}
=> false
[1,2,3].or?{|e| e>1}
=> true
[‘this’,‘that’].and?{|e|e.size==4}
=> true
[‘this’,‘that’].and?{|e|e=~/th/}
=> true
[‘this’,‘that’].and?{|e|e=~/at/}
=> false
[‘this’,‘that’].or?{|e|e=~/at/}
=> true
[proc{|a|a.size==4},proc{|a|a=~/th/}].and?{|e|e.call(“this”)}
=> true
[proc{|a|a.size==4},proc{|a|a=~/th/}].and?{|e|e.call(“these”)}
=> false

etc…

Then I discovered Array#detect because of a post by Jason Creighton, he
used it in his WrapHash class (btw, nice class Jason). So I run to irb
and played with #detect. It’s cool and I’ll be using it but it’s not a
replacement for my #and? and #or? methods. I still have to mess with
Array myself.

I looked for docs on Array#detect on http://www.rubycentral.com/ref/ and
I even looked in array.c (that was a big move for me). No luck. My
question is this: Where are these “extra” array methods documented? And
is there something already in Array that does what my #and? and #or?
methods do?

Michael Garriss

I believe that #any? and #all? are what you want.
They’re described in [1].

Andre

[1] http://www.whytheluckystiff.net/articles/2003/08/04/rubyOneEightOh

···

On Mon, 2003-08-25 at 15:17, mgarriss wrote:

And is there something already in Array that does what my #and? and #or?
methods do?

I have been using a little extension of Array that I wrote for awhile:

class Array
def and?() each{|e| return false unless yield e }; true end
def or?() each{|e| return true if yield e }; false end
end

Then I discovered Array#detect because of a post by Jason Creighton, he
used it in his WrapHash class (btw, nice class Jason).

Why thank you.

So I run to irb
and played with #detect. It’s cool and I’ll be using it but it’s not a
replacement for my #and? and #or? methods. I still have to mess with
Array myself.

Okay, any? and all? were mentioned in other posts but ‘ri’ is your
friend. The 1.8 version is on rdoc.sf.net, go to the download page and
scroll down to the bottom. Here’s what you can do with it:

~$ ri Enumerable
This is a test ‘ri’. Please report errors and omissions
on http://www.rubygarden.org/ruby?RIOnePointEight

···

On Tue, 26 Aug 2003 03:17:32 +0900 mgarriss mgarriss@earthlink.net wrote:


 module: Enumerable

 The Enumerable mixin provides collection classes with several
 traversal and searching methods, and with the ability to sort. The
 class must provide a method each, which yields successive members
 of the collection. If Enumerable#max, #min, or #sort is used, the
 objects in the collection must also implement a meaningful <=>
 operator, as these methods rely on an ordering between members of
 the collection.

 all?, any?, collect, detect, each_with_index, entries, find,
 find_all, grep, include?, inject, map, max, member?, min,
 partition, reject, select, sort, sort_by, to_a, zip

any? is a unique method so we don’t have to say “Enumerable#any?”

~$ ri any?
This is a test ‘ri’. Please report errors and omissions
on http://www.rubygarden.org/ruby?RIOnePointEight

-------------------------------------------------------- Enumerable#any?
enumObj.any? [{| obj | block } ] → true or false

 Passes each element of the collection to the given block. The
 method returns true if the block ever returns a value other that
 false or nil. If the block is not given, Ruby adds an implicit
 block of {|obj| obj} (that is any? will return true if at least one
 of the collection members is not false or nil.
    %w{ ant bear cat}.any? {|word| word.length >= 3}   #=> true
    %w{ ant bear cat}.any? {|word| word.length >= 4}   #=> true
    [ nil, true, 99 ].any?                             #=> true

~$ ri collect
This is a test ‘ri’. Please report errors and omissions
on http://www.rubygarden.org/ruby?RIOnePointEight

The method named `collect’ is not unique among Ruby’s classes and
modules:
Array#collect, Enumerable#collect

Oops, Array implements its own #collect, we have to be more specific

~$ ri Enumerable#collect
This is a test ‘ri’. Please report errors and omissions
on http://www.rubygarden.org/ruby?RIOnePointEight

----------------------------------------------------- Enumerable#collect
enumObj.collect {| obj | block } → anArray

 Returns a new array with the results of running block once for
 every element in enumObj.
    (1..4).collect {|i| i*i }   #=> [1, 4, 9, 16]
    (1..4).collect { "cat"  }   #=> ["cat", "cat", "cat", "cat"]

This incredibly handy tool is, of course, by Dave Thomas.

Jason Creighton

Andre Nathan wrote:

···

On Mon, 2003-08-25 at 15:17, mgarriss wrote:

And is there something already in Array that does what my #and? and #or?
methods do?

I believe that #any? and #all? are what you want.
They’re described in [1].

Andre

[1] http://www.whytheluckystiff.net/articles/2003/08/04/rubyOneEightOh

Ah! Thanks.

Michael

Andre Nathan wrote:

···

On Mon, 2003-08-25 at 15:17, mgarriss wrote:

And is there something already in Array that does what my #and? and #or?
methods do?

I believe that #any? and #all? are what you want.
They’re described in [1].

Andre

[1] http://www.whytheluckystiff.net/articles/2003/08/04/rubyOneEightOh

Now I see that #detect is in Enumerable. Doh!

Michael