Is this the DRYest way to do it in ruby?
<% for item in @items %>
<%= n = n + 1 rescue n = 1 %>
<% end %>
which initializes "n" to '1" and increments it as the loop progresses (and prints it out) since this is in one my app's views
Is this the DRYest way to do it in ruby?
<% for item in @items %>
<%= n = n + 1 rescue n = 1 %>
<% end %>
which initializes "n" to '1" and increments it as the loop progresses (and prints it out) since this is in one my app's views
You can use a ternary operator:
<% for item in @items %>
<%= n = n ? n+1 : 1 %>
<% end %>
But, depending on what you're trying to do, I'm guessing an each_with_index would be more appropriate
<% @items.each_with_index do |item, n| %>
<%= n %>
<% end %>