Noob - loop indexing

Discussion in 'Ruby' started by Simon Willard, Sep 28, 2009.

  1. I try to make Ruby loops using "each" like this:

    # a is an array
    a.each do ... end

    But I always end up needing the array index in the body of the loop for
    one reason or another. Then I change it to:

    i=0;
    while i<a.length do
    ...
    i+=1
    end

    But this feels clunky and less Ruby-like. Is there a way to access the
    index in an "each" loop? Is there a more elegant way to create an
    indexed loop?
    --
    Posted via http://www.ruby-forum.com/.
     
    Simon Willard, Sep 28, 2009
    #1
    1. Advertising

  2. Simon Willard, Sep 28, 2009
    #2
    1. Advertising

  3. Simon Willard

    Paul Smith Guest

    On Mon, Sep 28, 2009 at 9:46 PM, Paul Smith <> wrote=
    :
    > .
    > On Mon, Sep 28, 2009 at 9:43 PM, Simon Willard <> w=

    rote:
    >> I try to make Ruby loops using "each" like this:
    >>
    >> # a is an array
    >> a.each do =A0... =A0end

    >
    > a.each_index do |i|
    > =A0a...
    > end


    Or

    a.each_with_index do |x, i|
    #here, x is a
    puts x,i
    end
     
    Paul Smith, Sep 28, 2009
    #3
  4. On Sep 28, 2009, at 4:49 PM, Paul Smith wrote:
    > On Mon, Sep 28, 2009 at 9:46 PM, Paul Smith
    > <> wrote:
    >> On Mon, Sep 28, 2009 at 9:43 PM, Simon Willard <
    >> > wrote:
    >>> I try to make Ruby loops using "each" like this:
    >>>
    >>> # a is an array
    >>> a.each do ... end

    >>
    >> a.each_index do |i|
    >> a...
    >> end

    >
    > Or
    >
    > a.each_with_index do |x, i|
    > #here, x is a
    > puts x,i
    > end


    But just as importantly, ask yourself the question:
    "Why do I need the index? Am I not letting the objects do their
    thing?"


    On Sep 28, 2009, at 4:43 PM, Simon Willard wrote:
    > But this feels clunky and less Ruby-like. Is there a way to access
    > the
    > index in an "each" loop? Is there a more elegant way to create an
    > indexed loop?


    If your question were a bit different, then the answer might be:

    a.length.times do |i|
    #...
    end

    or even

    (0...a.length).each do |i|
    #...
    end
    # and note the use of the ... range constructor that excludes its end.

    (and, of course, it's up to you to formulate the appropriate
    question ;-)

    -Rob

    Rob Biedenharn http://agileconsultingllc.com
     
    Rob Biedenharn, Sep 28, 2009
    #4
    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. C
    Replies:
    0
    Views:
    503
  2. Emin
    Replies:
    4
    Views:
    411
    Paul McGuire
    Jan 12, 2007
  3. Skybuck Flying
    Replies:
    30
    Views:
    1,107
    Bill Reid
    Sep 19, 2011
  4. C
    Replies:
    3
    Views:
    223
    Manohar Kamath [MVP]
    Oct 17, 2003
  5. Isaac Won
    Replies:
    9
    Views:
    382
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page