Is there a automatic counter variable?

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.

Hi --

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.

Thanks guys.

something like this?

  (2..46).each {|i|       puts "i is now: #{i}"   }

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

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!