From: Trans Date: 2008-03-01T03:32:43+09:00 Subject: Re: possible to optimize this? On Feb 29, 10:45 am, laredotornado wrote: > Hi, > > I am creating a variable "counter" to count the number of iterations. > But I get the funny feeling Ruby (I'm using 1.8) may already have some > kind of implicit counter in the loop. Does anyone know of a way the > below can be optimized? > > <% counter = 0 %> > <% @ec_orders.each do |ec_order| %> > > > <%=h ec_order.ship_first_name %> > <%=h ec_order.ship_last_name %> > <%=h ec_order.invoice_num %> > <%= link_to 'View', {:action => 'view', :id => > ec_order.id} %> > > <% counter += 1 %> > <% end %> There is no implicit counter. I've often wished there were --an 'it' keyword that can provide information about each iteration. But certain people don;t seem to like that (not sure why since $1, seems perfectly acceptable). But I'm also guessing that it might have too much of a speed hit on iterative loops. In any case there is each_with_index. Facets has #collect_with_index if you need it --which in 1.9, I believe becomes array.collect.with_index. T.