# Is there a automatic counter variable?

**URL:** <https://www.ruby-forum.com/t/is-there-a-automatic-counter-variable/112127>\
**Category:** Rails\
**Created:** [August 15, 2007, 6:22pm UTC](https://www.ruby-forum.com/t/is-there-a-automatic-counter-variable/112127 "2007-08-15T18:22:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sw0rdfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@sw0rdfish](https://www.ruby-forum.com/u/sw0rdfish)\
**Post date:** [August 15, 2007, 6:22pm UTC](https://www.ruby-forum.com/t/is-there-a-automatic-counter-variable/112127/1 "2007-08-15T18:22:09Z")

</div>

Does ruby have anything where if I’m going through a do-while loop, or  
a “3.times” loop or “for product in products” where there’s a variable  
that will tell me which iteration of the loop I’m on? Instead of  
havign to declare my own counter = 0 and then increment it each time?

Seems like something Rails would have… I can’t seem to find  
anything on it.

Thanks guys.

---

<div class="post-metadata">

**Author:** ![sw0rdfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@sw0rdfish](https://www.ruby-forum.com/u/sw0rdfish)\
**Post date:** [August 15, 2007, 6:30pm UTC](https://www.ruby-forum.com/t/is-there-a-automatic-counter-variable/112127/2 "2007-08-15T18:30:10Z")

</div>

> Does ruby have anything where if I’m going through a do-while loop, or  
> a “3.times” loop or “for product in products” where there’s a variable  
> that will tell me which iteration of the loop I’m on? Instead of  
> havign to declare my own counter = 0 and then increment it each time?

irb(main):001:0\> 5.times {|i| puts i }  
0  
1  
2  
3  
4

irb(main):002:0\> [‘one’, ‘two’, ‘three’].each\_with\_index {|i,e| puts i,  
e}  
one  
0  
two  
1  
three  
2

Don’t know if “for product in products” has anything, but you could  
always  
do “products.each\_with\_index”.

-philip

---

<div class="post-metadata">

**Author:** ![sw0rdfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@sw0rdfish](https://www.ruby-forum.com/u/sw0rdfish)\
**Post date:** [August 15, 2007, 6:35pm UTC](https://www.ruby-forum.com/t/is-there-a-automatic-counter-variable/112127/3 "2007-08-15T18:35:10Z")

</div>

sw0rdfish wrote:

> 

something like this?

(2…46).each {|i|  
puts “i is now: #{i}”  
}

---

<div class="post-metadata">

**Author:** ![sw0rdfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@sw0rdfish](https://www.ruby-forum.com/u/sw0rdfish)\
**Post date:** [August 15, 2007, 9:33pm UTC](https://www.ruby-forum.com/t/is-there-a-automatic-counter-variable/112127/4 "2007-08-15T21:33:32Z")

</div>

Hey guys…

Looks good, the with\_index is the option I was looking for… just  
looked so messy having to declare a counter variable in a view…  
thanks!

---

<div class="post-metadata">

**Author:** ![sw0rdfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@sw0rdfish](https://www.ruby-forum.com/u/sw0rdfish)\
**Post date:** [August 15, 2007, 6:35pm UTC](https://www.ruby-forum.com/t/is-there-a-automatic-counter-variable/112127/5 "2007-08-15T18:35:11Z")

</div>

Hi –

On Wed, 15 Aug 2007, sw0rdfish wrote:

> Does ruby have anything where if I’m going through a do-while loop, or  
> a “3.times” loop or “for product in products” where there’s a variable  
> that will tell me which iteration of the loop I’m on? Instead of  
> havign to declare my own counter = 0 and then increment it each time?
> 
> Seems like something Rails would have… I can’t seem to find  
> anything on it.

3.times {|i| puts “Iteration #{i}” }

Also:

%w{a b c d e f}.each\_with\_index {|letter,i|  
puts “#{letter} is at index #{i}”  
}

David

–

- Books:  
RAILS ROUTING (new! [http://www.awprofessional.com/title/0321509242](http://www.awprofessional.com/title/0321509242))  
RUBY FOR RAILS ([Ruby for Rails](http://www.manning.com/black))
- Ruby/Rails training  
& consulting: Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))
