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