Array#each - getting each element and the index

Discussion in 'Ruby' started by Pat Maddox, Jan 20, 2006.

  1. Pat Maddox

    Pat Maddox Guest

    Is there a way to get each element of an array, as well the index of
    that element? Something like
    a =3D %w[foo bar]
    a.each { |val, index| .... } # val =3D> 'foo', index =3D> 0

    If there's no way to do that, is it better to use each_index and then
    access the array?
    a.each_index do |i|
    puts i
    puts a
    end

    vs finding the index after you have the element
    a.each do |val|
    puts a.index(val)
    puts val
    end

    Intuitively I'd think the second way may be a bit slower than the
    first, because it has to search through the array for the element,
    rather than accessing the index directly. Maybe not though...I'd just
    like a bit of explanation on this.

    Thanks,
    Pat
    Pat Maddox, Jan 20, 2006
    #1
    1. Advertising

  2. On Jan 20, 2006, at 8:08 AM, Pat Maddox wrote:

    > Is there a way to get each element of an array, as well the index of
    > that element?


    You bet.

    > Something like
    > a = %w[foo bar]
    > a.each { |val, index| .... } # val => 'foo', index => 0


    a.each_with_index ...

    James Edward Gray II
    James Edward Gray II, Jan 20, 2006
    #2
    1. Advertising

  3. Pat Maddox wrote:
    > Is there a way to get each element of an array, as well the index of
    > that element? Something like
    > a = %w[foo bar]
    > a.each { |val, index| .... } # val => 'foo', index => 0


    a.each_with_index { |val, index| .... }

    robert
    Robert Klemme, Jan 20, 2006
    #3
  4. Pat Maddox

    ChrisH Guest

    Pat Maddox wrote:
    > Is there a way to get each element of an array, as well the index of
    > that element? Something like
    > a = %w[foo bar]
    > a.each { |val, index| .... } # val => 'foo', index => 0


    Yes, and suprisingly it called each_with_index 9^)

    Cheers
    ChrisH, Jan 20, 2006
    #4
  5. Pat Maddox

    Pat Maddox Guest

    On 1/20/06, James Edward Gray II <> wrote:
    > On Jan 20, 2006, at 8:08 AM, Pat Maddox wrote:
    >
    > > Is there a way to get each element of an array, as well the index of
    > > that element?

    >
    > You bet.
    >
    > > Something like
    > > a =3D %w[foo bar]
    > > a.each { |val, index| .... } # val =3D> 'foo', index =3D> 0

    >
    > a.each_with_index ...
    >
    > James Edward Gray II
    >
    >


    Perfect, thanks

    Is this using some idiom (e.g. attaching _with_index) that I don't
    know about? I didn't see that method in the Array rdoc.
    Pat Maddox, Jan 20, 2006
    #5
  6. On Jan 20, 2006, at 4:06 PM, Pat Maddox wrote:

    > Is this using some idiom (e.g. attaching _with_index) that I don't
    > know about? I didn't see that method in the Array rdoc.


    If you do a ``ri Array'' you will see that #each_with_index is mixed
    in from Enumerable. see Enumerable#each_with_index for more information.

    -- Daniel
    Daniel Harple, Jan 20, 2006
    #6
  7. Pat Maddox wrote:

    >
    > Is this using some idiom (e.g. attaching _with_index) that I don't
    > know about? I didn't see that method in the Array rdoc.
    >
    >


    It is a plain method name.

    Enumerable#each_with_index

    lopex
    Marcin Mielżyński, Jan 20, 2006
    #7
    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:
    2
    Views:
    240
  2. HANM
    Replies:
    2
    Views:
    697
    Joseph Kesselman
    Jan 29, 2008
  3. Shawn W_
    Replies:
    5
    Views:
    264
    Aldric Giacomoni
    Sep 16, 2009
  4. Tomasz Chmielewski

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

    Tomasz Chmielewski, Mar 4, 2008, in forum: Perl Misc
    Replies:
    4
    Views:
    269
    Tomasz Chmielewski
    Mar 4, 2008
  5. yawnmoth
    Replies:
    1
    Views:
    97
    Laurent Bugnion
    Jun 24, 2006
Loading...

Share This Page