Extra Array methods?

Discussion in 'Ruby' started by mgarriss, Aug 25, 2003.

  1. mgarriss

    mgarriss Guest

    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
     
    mgarriss, Aug 25, 2003
    #1
    1. Advertising

  2. mgarriss

    Andre Nathan Guest

    Andre Nathan, Aug 25, 2003
    #2
    1. Advertising

  3. mgarriss

    mgarriss Guest

    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
     
    mgarriss, Aug 25, 2003
    #3
  4. mgarriss

    mgarriss Guest

    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
     
    mgarriss, Aug 25, 2003
    #4
  5. 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
     
    Jason Creighton, Aug 26, 2003
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    6
    Views:
    286
    Jim Langston
    Oct 29, 2006
  2. mathieu
    Replies:
    3
    Views:
    599
    Bo Persson
    Sep 4, 2009
  3. James Edward Gray II

    Mini-RCR: Extra Argument for Array#join

    James Edward Gray II, Dec 31, 2006, in forum: Ruby
    Replies:
    12
    Views:
    186
    Trans
    Jan 3, 2007
  4. Kenneth McDonald
    Replies:
    5
    Views:
    325
    Kenneth McDonald
    Sep 26, 2008
  5. Ian Pilcher

    Static array with #defined # of "extra" members

    Ian Pilcher, Aug 4, 2012, in forum: C Programming
    Replies:
    4
    Views:
    484
    Ian Pilcher
    Aug 4, 2012
Loading...

Share This Page