I am using a ruby iterator on a view in a rails app like so:
<% (1..@document.data.length).each_with_index do |element, index| %>
...
<% end %>
I thought the addition of the 1.. instead of just saying:
@document.data
would get the trick of having the index above start at 1. But alas, the above code index is still 0 to data.length (-1 effectively). So what am I doing wrong, i need the index to equal 1-data.length…no clue how to set up the iterator to do this.
I think maybe you misunderstand
each_with_index.eachwill iterate over elements in an arraywhich outputs;
each_with_indexiterates over the elements, and also passes in the index (starting from zero)which outputs
if you want it 1-indexed then just add 1.
which outputs
if you want to iterate over a subset of an array, then just choose the subset, then iterate over it