possible to optimize this?

L

laredotornado

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
 
M

Matthew Moss

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?

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 %>
 
F

Florian Gilcher

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

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

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

Greetings
Florian Gilcher
 
T

Trans

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.

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

Rados³aw Bu³at

PiAgVGhlcmUgaXMgbm8gaW1wbGljaXQgY291bnRlci4gSSd2ZSBvZnRlbiB3aXNoZWQgdGhlcmUg
d2VyZSAtLWFuICdpdCcKPiAga2V5d29yZCB0aGF0IGNhbiBwcm92aWRlIGluZm9ybWF0aW9uIGFi
b3V0IGVhY2ggaXRlcmF0aW9uLiBCdXQgY2VydGFpbgo+ICBwZW9wbGUgZG9uO3Qgc2VlbSB0byBs
aWtlIHRoYXQgKG5vdCBzdXJlIHdoeSBzaW5jZSAkMSwgc2VlbXMgcGVyZmVjdGx5Cj4gIGFjY2Vw
dGFibGUpLiBCdXQgSSdtIGFsc28gZ3Vlc3NpbmcgdGhhdCBpdCBtaWdodCBoYXZlIHRvbyBtdWNo
IG9mIGEKPiAgc3BlZWQgaGl0IG9uIGl0ZXJhdGl2ZSBsb29wcy4KPgoKTm8gb2ZmZW5zZSBidXQg
aXQncyBtb3N0IHN0dXBpZCBpZGVhIEkndmUgaGVhcmQgbGF0ZWx5LiBSdWJ5IGN1cnJlbnRseQp0
cnkgZXNjYXBlIGZyb20gbWFnaWMgZ2xvYmFsIHZhcmlhYmxlcyBib3Jyb3dlZCBmcm9tIFBlcmwg
KHVzZWQgZm9yCmV4YW1wbGUgaW4gcmVnZXhwKS4gVXNpbmcgLmVhY2hfd2l0aF9pbmRleCB3aXRo
IGV4cGxpY2l0IG5hbWUgZm9yCml0ZXJhdGlvbiBpbmRleCBpcyBhcyBzaW1wbGUgYXMgIm1hZ2lj
IHZlcnNpb24iIGFuZCBpcyBtdWNoIG1vcmUKcmVhZGFibGUuCgoKLS0gClJhZG9zs2F3IEJ1s2F0
CgpodHRwOi8vcmFkYXJlay5qb2dnZXIucGwgLSBt82ogYmxvZwo=
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,285
Messages
2,571,416
Members
48,108
Latest member
AmeliaAmad

Latest Threads

Top