Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Extra Array methods?

Reply
Thread Tools

Extra Array methods?

 
 
mgarriss
Guest
Posts: n/a
 
      08-25-2003
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


 
Reply With Quote
 
 
 
 
Andre Nathan
Guest
Posts: n/a
 
      08-25-2003
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/arti...rubyOneEightOh


 
Reply With Quote
 
 
 
 
mgarriss
Guest
Posts: n/a
 
      08-25-2003
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/arti...rubyOneEightOh
>


Ah! Thanks.

Michael


 
Reply With Quote
 
mgarriss
Guest
Posts: n/a
 
      08-25-2003
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/arti...rubyOneEightOh
>


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

Michael


 
Reply With Quote
 
Jason Creighton
Guest
Posts: n/a
 
      08-26-2003
On Tue, 26 Aug 2003 03:17:32 +0900
mgarriss <> wrote:

> 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


<snip>

> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Static array with #defined # of "extra" members Ian Pilcher C Programming 4 08-04-2012 03:00 PM
Does return-by-value mean extra copies and extra overhead? mathieu C++ 3 09-04-2009 04:25 PM
const and array of array (of array ...) Mara Guida C Programming 3 09-03-2009 07:54 AM
Mini-RCR: Extra Argument for Array#join James Edward Gray II Ruby 12 01-03-2007 04:30 PM
User inputted length of an array without an extra variable. foreverbored75@gmail.com C++ 6 10-29-2006 12:44 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57