# Possible to optimize this?

**URL:** <https://www.ruby-forum.com/t/possible-to-optimize-this/130584>\
**Category:** Ruby\
**Created:** [February 29, 2008, 4:46pm UTC](https://www.ruby-forum.com/t/possible-to-optimize-this/130584 "2008-02-29T16:46:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![laredotornado](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9dc877/32.png) [@laredotornado](https://www.ruby-forum.com/u/laredotornado)\
**Post date:** [February 29, 2008, 4:46pm UTC](https://www.ruby-forum.com/t/possible-to-optimize-this/130584/1 "2008-02-29T16:46:08Z")

</div>

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

Thanks, - Dave

---

<div class="post-metadata">

**Author:** ![laredotornado](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9dc877/32.png) [@laredotornado](https://www.ruby-forum.com/u/laredotornado)\
**Post date:** [February 29, 2008, 4:53pm UTC](https://www.ruby-forum.com/t/possible-to-optimize-this/130584/2 "2008-02-29T16:53:21Z")

</div>

On Feb 29, 10:45 am, laredotornado [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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?

each\_with\_index, part of the Enumerable module.

\<% @ec\_orders.each\_with\_index do |ec\_order, index| %\>

 \> \<%=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} %\> \<% end %\>

---

<div class="post-metadata">

**Author:** ![laredotornado](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9dc877/32.png) [@laredotornado](https://www.ruby-forum.com/u/laredotornado)\
**Post date:** [February 29, 2008, 4:54pm UTC](https://www.ruby-forum.com/t/possible-to-optimize-this/130584/3 "2008-02-29T16:54:32Z")

</div>

On Feb 29, 2008, at 4:45 PM, laredotornado wrote:

> \<%=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 %\>
> 
> Thanks, - Dave

Use “each\_with\_index do |order, counter|” instead.

[http://www.ruby-doc.org/core/classes/Enumerable.html#M003168](http://www.ruby-doc.org/core/classes/Enumerable.html#M003168)

Greetings  
Florian G.

---

<div class="post-metadata">

**Author:** ![laredotornado](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9dc877/32.png) [@laredotornado](https://www.ruby-forum.com/u/laredotornado)\
**Post date:** [February 29, 2008, 7:33pm UTC](https://www.ruby-forum.com/t/possible-to-optimize-this/130584/4 "2008-02-29T19:33:33Z")

</div>

On Feb 29, 10:45 am, laredotornado [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> <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} %\>
> 
> \<% 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.

---

<div class="post-metadata">

**Author:** ![laredotornado](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9dc877/32.png) [@laredotornado](https://www.ruby-forum.com/u/laredotornado)\
**Post date:** [March 1, 2008, 10:00pm UTC](https://www.ruby-forum.com/t/possible-to-optimize-this/130584/5 "2008-03-01T22:00:19Z")

</div>

> 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](http://radarek.jogger.pl) - mój blog

---

<div class="post-metadata">

**Author:** ![laredotornado](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9dc877/32.png) [@laredotornado](https://www.ruby-forum.com/u/laredotornado)\
**Post date:** [March 3, 2008, 10:57am UTC](https://www.ruby-forum.com/t/possible-to-optimize-this/130584/6 "2008-03-03T10:57:14Z")

</div>

2008/3/1, Rados³aw Bu³at [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

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