# Counting in for .. in

**URL:** <https://www.ruby-forum.com/t/counting-in-for-in/130693>\
**Category:** Ruby\
**Created:** [March 1, 2008, 11:24pm UTC](https://www.ruby-forum.com/t/counting-in-for-in/130693 "2008-03-01T23:24:41Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![liberte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9e8a1a/32.png) [@liberte](https://www.ruby-forum.com/u/liberte)\
**Post date:** [March 1, 2008, 11:24pm UTC](https://www.ruby-forum.com/t/counting-in-for-in/130693/1 "2008-03-01T23:24:41Z")

</div>

Hi,

i’m doing my first steps in ruby (on rails) and often use things like

for thing in @several\_things  
print thing  
end

Ok so far, looks great 🙂

But i often need a counter inside of the loop, may it be to create row  
colors or other stuff. What i end up with then is:

i = 0  
for thing in @several\_things  
print thing + " No. " + i.to\_s  
i += 1  
end

Pardon me, but this is ugly, compared to the first clean lines. So far i  
know ruby as an elegant language, so i wouldn’t be surpised if there’d  
be any kind of implemented counter in the for loop or a smarter way to  
implement it.

Any hints?

Thanks a lot for any help!  
R.D.

---

<div class="post-metadata">

**Author:** ![liberte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9e8a1a/32.png) [@liberte](https://www.ruby-forum.com/u/liberte)\
**Post date:** [March 1, 2008, 11:35pm UTC](https://www.ruby-forum.com/t/counting-in-for-in/130693/2 "2008-03-01T23:35:21Z")

</div>

On Sun, Mar 02, 2008 at 07:24:34AM +0900, Daniel L. wrote:

> i’m doing my first steps in ruby (on rails) and often use things like
> 
> for thing in @several\_things  
> print thing  
> end  
> […]

For reasons I find it difficult to express the for-in construct is  
frowned  
upon. There are better and more flexible iteration constructs, and it  
looks  
like you are starting to feel the need for them.

> But i often need a counter inside of the loop, may it be to create row  
> colors or other stuff. What i end up with then is:
> 
> i = 0  
> for thing in @several\_things  
> print thing + " No. " + i.to\_s  
> i += 1  
> end  
> […]

This is where each\_with\_index, one of those more flexible iteration  
constructs, comes in.

@several\_thing.each\_with\_index do |thing,i|  
print thing + " No. " + i.to\_s  
i += 1  
end

You should be familiar with ri for reading Ruby API documentation. Look  
up  
Enumerable for the wide variety of iteration/enumeration constructs Ruby  
gives you right out of the box. There are even more advanced constructs  
available from other libraries (e.g. facets).

> Thanks a lot for any help!  
> R.D.  
> –Greg

---

<div class="post-metadata">

**Author:** ![liberte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9e8a1a/32.png) [@liberte](https://www.ruby-forum.com/u/liberte)\
**Post date:** [March 1, 2008, 11:37pm UTC](https://www.ruby-forum.com/t/counting-in-for-in/130693/3 "2008-03-01T23:37:07Z")

</div>

Daniel L. wrote:

> But i often need a counter inside of the loop, may it be to create row  
> Any hints?
> 
> Thanks a lot for any help!  
> R.D.

C:\Documents and Settings\CynicalRyan\>cat test.rb  
@things = [“one”, “two”, “three”]

@things.each\_with\_index do |thing,i|  
puts thing  
puts i  
end  
C:\Documents and Settings\CynicalRyan\>ruby -v test.rb  
ruby 1.8.6 (2007-03-13 patchlevel 0) [i386-mswin32]  
one  
0  
two  
1  
three  
2

---

<div class="post-metadata">

**Author:** ![liberte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9e8a1a/32.png) [@liberte](https://www.ruby-forum.com/u/liberte)\
**Post date:** [March 1, 2008, 11:39pm UTC](https://www.ruby-forum.com/t/counting-in-for-in/130693/4 "2008-03-01T23:39:28Z")

</div>

On Sat, Mar 1, 2008 at 11:24 PM, Daniel L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hi,
> 
> i’m doing my first steps in ruby (on rails) and often use things like
> 
> for thing in @several\_things  
> print thing  
> end

Let’s start with equivalent code:

@several\_things.each do |thing|  
print thing  
end

> Ok so far, looks great 🙂
> 
> But i often need a counter inside of the loop, may it be to create row  
> colors or other stuff. What i end up with then is:
> 
> i = 0  
> for thing in @several\_things  
> print thing + " No. " + i.to\_s  
> i += 1  
> end

Now let me introduce Enumerable module, that contains each\_with\_index:

@several\_things.each\_with\_index do |thing, i|  
print “#{thing} No. #{i}”  
end

Note: #{expression} is replaced with expression.to\_s (more or less)  
Lookup Enumerable in the docs, it contains lots of useful stuff.

---

<div class="post-metadata">

**Author:** ![liberte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9e8a1a/32.png) [@liberte](https://www.ruby-forum.com/u/liberte)\
**Post date:** [March 1, 2008, 11:43pm UTC](https://www.ruby-forum.com/t/counting-in-for-in/130693/5 "2008-03-01T23:43:34Z")

</div>

On 3/1/08, Daniel L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> But i often need a counter inside of the loop, may it be to create row  
> be any kind of implemented counter in the for loop or a smarter way to  
> implement it.

Simple. Use #each\_with\_index instead of for… in:

@several\_things.each\_with\_index do |thing, index|  
…  
end

Read up on Enumerable. It’s pretty handy:  
[http://www.ruby-doc.org/core/classes/Enumerable.html](http://www.ruby-doc.org/core/classes/Enumerable.html)

Christopher

---

<div class="post-metadata">

**Author:** ![liberte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9e8a1a/32.png) [@liberte](https://www.ruby-forum.com/u/liberte)\
**Post date:** [March 2, 2008, 6:50am UTC](https://www.ruby-forum.com/t/counting-in-for-in/130693/6 "2008-03-02T06:50:30Z")

</div>

On Mar 1, 4:24 pm, Daniel L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> But i often need a counter inside of the loop, may it be to create row  
> be any kind of implemented counter in the for loop or a smarter way to  
> implement it.

E:\>irb --prompt xmp  
a = %w(zero one two three)  
==\>[“zero”, “one”, “two”, “three”]  
a.each\_with\_index{|x,i| puts “Item no. #{i} is #{x}.”}  
Item no. 0 is zero.  
Item no. 1 is one.  
Item no. 2 is two.  
Item no. 3 is three.  
==\>[“zero”, “one”, “two”, “three”]

---

<div class="post-metadata">

**Author:** ![liberte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9e8a1a/32.png) [@liberte](https://www.ruby-forum.com/u/liberte)\
**Post date:** [March 2, 2008, 6:39am UTC](https://www.ruby-forum.com/t/counting-in-for-in/130693/7 "2008-03-02T06:39:54Z")

</div>

Gregory S. wrote:

> On Sun, Mar 02, 2008 at 07:24:34AM +0900, Daniel L. wrote:
> 
> > i’m doing my first steps in ruby (on rails) and often use things like
> > 
> > for thing in @several\_things  
> > print thing  
> > end  
> > […]
> 
> For reasons I find it difficult to express the for-in construct is  
> frowned  
> upon. There are better and more flexible iteration constructs, and it  
> looks  
> like you are starting to feel the need for them.
> 
> > But i often need a counter inside of the loop, may it be to create row  
> > colors or other stuff. What i end up with then is:
> > 
> > i = 0  
> > for thing in @several\_things  
> > print thing + " No. " + i.to\_s  
> > i += 1  
> > end  
> > […]
> 
> This is where each\_with\_index, one of those more flexible iteration  
> constructs, comes in.
> 
> @several\_thing.each\_with\_index do |thing,i|  
> print thing + " No. " + i.to\_s  
> i += 1  
> end

Rather that should be:

arr = [‘a’, ‘b’, ‘c’]

arr.each\_with\_index do |elmt, i|  
puts “Element at index #{i} is: #{elmt}”  
end

–output:–  
Element at index 0 is: a  
Element at index 1 is: b  
Element at index 2 is: c

---

<div class="post-metadata">

**Author:** ![liberte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9e8a1a/32.png) [@liberte](https://www.ruby-forum.com/u/liberte)\
**Post date:** [March 2, 2008, 6:56am UTC](https://www.ruby-forum.com/t/counting-in-for-in/130693/8 "2008-03-02T06:56:22Z")

</div>

William J. wrote:

> > know ruby as an elegant language, so i wouldn’t be surpised if there’d  
> > Item no. 1 is one.  
> > Item no. 2 is two.  
> > Item no. 3 is three.  
> > ==\>[“zero”, “one”, “two”, “three”]

puts a.zip((0…a.size).to\_a).map{|a| “Item #{a[1]} is #{a[0]}”}  
Item 0 is zero  
Item 1 is one  
Item 2 is two  
Item 3 is three

---

<div class="post-metadata">

**Author:** ![liberte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9e8a1a/32.png) [@liberte](https://www.ruby-forum.com/u/liberte)\
**Post date:** [March 3, 2008, 12:19pm UTC](https://www.ruby-forum.com/t/counting-in-for-in/130693/9 "2008-03-03T12:19:17Z")

</div>

Jano S. schrieb:

> @several\_things.each do |thing|  
> print thing  
> end

I’m really puzzled why Rails doesn’t use the Ruby idiom for the  
templates …

- Matthias
