views:

44

answers:

2

My google skills are failing me big time. If I have a standary RoR loop like this:

<% @notes.each do |q| %>
<% end>

How do I access a loop counter from inside the loop? Thanks for reading.

+1  A: 

There is no loop counter in the example given. In other languages, this style of loop is usually called a foreach loop. You can still access the current item of the collection by using the variable q in the example given.

Matthew Scharley
+3  A: 

Use each_with_index instead of each to get the index:

@notes.each_with_index do |note, idx|
 p idx
 p note
end

Refer to the ruby documentation for more details.

KandadaBoggu