Extra Array methods?

M

mgarriss

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
 
J

Jason Creighton

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

<blush>

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

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top