From: Phillip Gawlowski Date: 2008-03-02T07:35:41+09:00 Subject: Re: counting in for .. in .. Daniel Liebig 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 > > 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 > > [..] > > 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 -- Phillip Gawlowski