Incrementing and initializing counter in a view in 1 (one!) line

Ruby On Rails Add comments

Many times I have to make a counter that starts with 1 and monotonically incremented by 1 in Rails view. The typical example is when showing a list of things like:

7.1
7.2
7.3
....

The nice trick is to make it in 1 line:

<li>7.<%= n = n + 1 rescue n = 1 %></li>

instead of initializing “n” before the loop (which requres extra line of code).

Now the question is: is this DRY enough? Is it?

Leave a Reply

WP Theme by Ray Hosting and brought to you by Ray Creations | Wordpress Themes
CSS XHTML