Is there anything like Array#Index that takes a block?

Discussion in 'Ruby' started by Lee Griffiths, Apr 28, 2009.

  1. I would like to avoid doing :
    Array.index(Enumerable.find { |blarg| blarg =~ /blah/ })

    as that traverses the array twice. Is there any built in method that
    does this for me? That is, it's a method that takes a block and returns
    the index of the first element for which this block returns true?

    Thanks
    --
    Posted via http://www.ruby-forum.com/.
     
    Lee Griffiths, Apr 28, 2009
    #1
    1. Advertising

  2. Lee Griffiths

    badboy Guest

    Lee Griffiths schrieb:
    > I would like to avoid doing :
    > Array.index(Enumerable.find { |blarg| blarg =~ /blah/ })
    >
    > as that traverses the array twice. Is there any built in method that
    > does this for me? That is, it's a method that takes a block and returns
    > the index of the first element for which this block returns true?
    >
    > Thanks

    yes... it's called Array#index *scnr*
    ri says:
    array.index(obj) -> int or nil
    array.index {|item| block} -> int or nil
    ...
    If a block is given instead of an argument, returns first
    object for which _block_ is true.

    so...just use #index with a blocck
     
    badboy, Apr 28, 2009
    #2
    1. Advertising

  3. badboy wrote:
    > Lee Griffiths schrieb:
    >> I would like to avoid doing :
    >> Array.index(Enumerable.find { |blarg| blarg =~ /blah/ })
    >>
    >> as that traverses the array twice. Is there any built in method that
    >> does this for me? That is, it's a method that takes a block and returns
    >> the index of the first element for which this block returns true?
    >>
    >> Thanks

    > yes... it's called Array#index *scnr*
    > ri says:
    > array.index(obj) -> int or nil
    > array.index {|item| block} -> int or nil
    > ...
    > If a block is given instead of an argument, returns first
    > object for which _block_ is true.
    >
    > so...just use #index with a blocck


    Which version of Ruby is that for? My ri, irb and ruby don't confirm
    this. I'm using Ruby 1.8.6. Additionally http://www.ruby-doc.org/core/
    claims that Array#index only has a single implementation.
    --
    Posted via http://www.ruby-forum.com/.
     
    Lee Griffiths, Apr 28, 2009
    #3
  4. Lee Griffiths

    badboy Guest

    Lee Griffiths schrieb:
    > badboy wrote:
    >> Lee Griffiths schrieb:
    >>> I would like to avoid doing :
    >>> Array.index(Enumerable.find { |blarg| blarg =~ /blah/ })
    >>>
    >>> as that traverses the array twice. Is there any built in method that
    >>> does this for me? That is, it's a method that takes a block and returns
    >>> the index of the first element for which this block returns true?
    >>>
    >>> Thanks

    >> yes... it's called Array#index *scnr*
    >> ri says:
    >> array.index(obj) -> int or nil
    >> array.index {|item| block} -> int or nil
    >> ...
    >> If a block is given instead of an argument, returns first
    >> object for which _block_ is true.
    >>
    >> so...just use #index with a blocck

    >
    > Which version of Ruby is that for? My ri, irb and ruby don't confirm
    > this. I'm using Ruby 1.8.6. Additionally http://www.ruby-doc.org/core/
    > claims that Array#index only has a single implementation.

    hm...oh =/
    ruby 1.8.7 (and 1.9.1, too)
     
    badboy, Apr 28, 2009
    #4
  5. Lee Griffiths

    7stud -- Guest

    arr = %w{apple banana blarg blueberry}

    pos = nil
    arr.each_with_index do |str, i|
    if str =~ /bl/
    pos = i
    break
    end

    end

    puts pos

    --output:--
    2

    --
    Posted via http://www.ruby-forum.com/.
     
    7stud --, Apr 28, 2009
    #5
  6. Lee Griffiths

    Robert Dober Guest

    > =A0If a block is given instead of an argument, returns first
    > =A0 =A0 object for which _block_ is true.

    I could not believe it says that, probably should provide a patch.

    OP do not worry, of course it returns the index and not the object.
    HTH
    R.
     
    Robert Dober, Apr 28, 2009
    #6
  7. Lee Griffiths

    badboy Guest

    Robert Dober schrieb:
    >> If a block is given instead of an argument, returns first
    >> object for which _block_ is true.

    > I could not believe it says that, probably should provide a patch.
    >
    > OP do not worry, of course it returns the index and not the object.
    > HTH
    > R.
    >

    ah! yeah, I saw that, too but missed to mention it. It's just a
    documentation mistake
     
    badboy, Apr 28, 2009
    #7
  8. Lee Griffiths

    Robert Dober Guest

    On Tue, Apr 28, 2009 at 4:19 PM, badboy <> wrote:
    > Robert Dober schrieb:
    >>> =A0If a block is given instead of an argument, returns first
    >>> =A0 =A0 object for which _block_ is true.

    >> I could not believe it says that, probably should provide a patch.
    >>
    >> OP do not worry, of course it returns the index and not the object.
    >> HTH
    >> R.
    >>

    > ah! yeah, I saw that, too but missed to mention it. It's just a
    > documentation mistake

    yes indeed, but do you not think this is bad? I had to fire up irb to
    check so have done billions of billions of Ruby users in the
    universe...
    Think big!
    R.
     
    Robert Dober, Apr 28, 2009
    #8
  9. Lee Griffiths

    Roger Pack Guest

    For followers, I believe the acceptable backport for 1.8.6 is:


    if RUBY_VERSION < '1.8.7'
    class Array
    alias original_index index
    def index *args

    if args.length > 0
    return original_index(*args)
    else
    pos = nil
    each_with_index do |element, i|
    if yield(element)
    return i
    end
    end
    return nil
    end
    end
    end
    end

    --
    Posted via http://www.ruby-forum.com/.
     
    Roger Pack, Feb 13, 2011
    #9
    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:
    10
    Views:
    1,345
    Malte
    Jun 30, 2005
  2. Edward Faulkner

    Block that takes a block

    Edward Faulkner, Sep 20, 2005, in forum: Ruby
    Replies:
    1
    Views:
    127
    Brian Mitchell
    Sep 20, 2005
  3. Thufir
    Replies:
    1
    Views:
    139
    7stud --
    Oct 20, 2007
  4. Bil Kleb
    Replies:
    5
    Views:
    124
  5. Tomasz Chmielewski

    sorting index-15, index-9, index-110 "the human way"?

    Tomasz Chmielewski, Mar 4, 2008, in forum: Perl Misc
    Replies:
    4
    Views:
    307
    Tomasz Chmielewski
    Mar 4, 2008
Loading...

Share This Page