Possible to optimize this?

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| %>
  <tr <%= classify(counter) %>>
    <td><%=h ec_order.ship_first_name %></td>
    <td><%=h ec_order.ship_last_name %></td>
    <td><%=h ec_order.invoice_num %></td>
    <td align="center"><%= link_to 'View', {:action => 'view', :id =>
ec_order.id} %></td>
  </tr>
  <% counter += 1 %>
<% end %>

Thanks, - Dave

each_with_index, part of the Enumerable module.

<% @ec_orders.each_with_index do |ec_order, index| %>
  <tr <%= classify(index) %>>
    <td><%=h ec_order.ship_first_name %></td>
    <td><%=h ec_order.ship_last_name %></td>
    <td><%=h ec_order.invoice_num %></td>
    <td align="center"><%= link_to 'View', {:action => 'view', :id =>
ec_order.id} %></td>
  </tr>
<% end %>

···

On Feb 29, 10:45 am, laredotornado <laredotorn...@zipmail.com> 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?

Use "each_with_index do |order, counter|" instead.

http://www.ruby-doc.org/core/classes/Enumerable.html#M003168

Greetings
Florian Gilcher

···

On Feb 29, 2008, at 4:45 PM, 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| %>
<tr <%= classify(counter) %>>
   <td><%=h ec_order.ship_first_name %></td>
   <td><%=h ec_order.ship_last_name %></td>
   <td><%=h ec_order.invoice_num %></td>
   <td align="center"><%= link_to 'View', {:action => 'view', :id =>
ec_order.id} %></td>
</tr>
<% counter += 1 %>
<% end %>

Thanks, - Dave

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.

···

On Feb 29, 10:45 am, laredotornado <laredotorn...@zipmail.com> 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| %>
  <tr <%= classify(counter) %>>
    <td><%=h ec_order.ship_first_name %></td>
    <td><%=h ec_order.ship_last_name %></td>
    <td><%=h ec_order.invoice_num %></td>
    <td align="center"><%= link_to 'View', {:action => 'view', :id =>
ec_order.id} %></td>
  </tr>
  <% 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.

No offense but it's most stupid idea I've heard lately. Ruby currently
try escape from magic global variables borrowed from Perl (used for
example in regexp). Using .each_with_index with explicit name for
iteration index is as simple as "magic version" and is much more
readable.

···

--
Radosław Bułat

http://radarek.jogger.pl - mój blog

> There is no implicit counter. I've often wished there were --an 'it'

I find this statement a bit strange in the light of Enumerable#each_with_index.

> 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.

No offense but it's most stupid idea I've heard lately.

Please watch your language.

Ruby currently
try escape from magic global variables borrowed from Perl (used for
example in regexp). Using .each_with_index with explicit name for
iteration index is as simple as "magic version" and is much more
readable.

Also, $1 is already taken (regexp captured group 1).

Kind regards

robert

···

2008/3/1, Radosław Bułat <radek.bulat@gmail.com>:

--
use.inject do |as, often| as.you_can - without end