# Noob - loop indexing

**URL:** <https://www.ruby-forum.com/t/noob-loop-indexing/176310>\
**Category:** Ruby\
**Created:** [September 28, 2009, 10:43pm UTC](https://www.ruby-forum.com/t/noob-loop-indexing/176310 "2009-09-28T22:43:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![simon1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@simon1](https://www.ruby-forum.com/u/simon1)\
**Post date:** [September 28, 2009, 10:43pm UTC](https://www.ruby-forum.com/t/noob-loop-indexing/176310/1 "2009-09-28T22:43:28Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![simon1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@simon1](https://www.ruby-forum.com/u/simon1)\
**Post date:** [September 28, 2009, 10:48pm UTC](https://www.ruby-forum.com/t/noob-loop-indexing/176310/2 "2009-09-28T22:48:54Z")

</div>

Paul S. wrote:

> .

Thank you.

---

<div class="post-metadata">

**Author:** ![simon1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@simon1](https://www.ruby-forum.com/u/simon1)\
**Post date:** [September 28, 2009, 10:46pm UTC](https://www.ruby-forum.com/t/noob-loop-indexing/176310/3 "2009-09-28T22:46:51Z")

</div>

.  
On Mon, Sep 28, 2009 at 9:43 PM, Simon W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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[i]…  
end

–  
Paul S.  
[http://www.nomadicfun.co.uk](http://www.nomadicfun.co.uk)

removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![simon1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@simon1](https://www.ruby-forum.com/u/simon1)\
**Post date:** [September 28, 2009, 10:50pm UTC](https://www.ruby-forum.com/t/noob-loop-indexing/176310/4 "2009-09-28T22:50:22Z")

</div>

On Mon, Sep 28, 2009 at 9:46 PM, Paul S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> .  
> On Mon, Sep 28, 2009 at 9:43 PM, Simon W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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[i]…  
> end

Or

a.each\_with\_index do |x, i|  
#here, x is a[i]  
puts x,i  
end

---

<div class="post-metadata">

**Author:** ![simon1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@simon1](https://www.ruby-forum.com/u/simon1)\
**Post date:** [September 28, 2009, 11:07pm UTC](https://www.ruby-forum.com/t/noob-loop-indexing/176310/5 "2009-09-28T23:07:56Z")

</div>

On Sep 28, 2009, at 4:49 PM, Paul S. wrote:

> > a[i]…  
> > end
> 
> Or
> 
> a.each\_with\_index do |x, i|  
> #here, x is a[i]  
> 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 W. 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 B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid
