each_with_index_do starting at 1 for index

This may not be exactly the same each_with_index method in question, but I think the result may close to something in mod is asking...

%w(a b c).each.with_index(1) { |item, index| puts "#{index} - #{item}" }

# 1 - a
# 2 - b
# 3 - c

For more information https://ruby-doc.org/core-2.6.1/Enumerator.html#method-i-with_index


I think maybe you misunderstand each_with_index.

each will iterate over elements in an array

[:a, :b, :c].each do |object|
  puts object
end

which outputs;

:a
:b
:c

each_with_index iterates over the elements, and also passes in the index (starting from zero)

[:a, :b, :c].each_with_index do |object, index|
  puts "#{object} at index #{index}"
end

which outputs

:a at index 0
:b at index 1
:c at index 2

if you want it 1-indexed then just add 1.

[:a, :b, :c].each_with_index do |object, index|
  indexplusone = index + 1
  puts "#{object} at index #{indexplusone}"
end

which outputs

:a at index 1
:b at index 2
:c at index 3

if you want to iterate over a subset of an array, then just choose the subset, then iterate over it

without_first_element = array[1..-1]

without_first_element.each do |object|
  ...
end

Unless you're using an older Ruby like 1.8 (I think this was added in 1.9 but I'm not sure), you can use each.with_index(1) to get a 1-based enumerator:

In your case it would be like this:

<% @document.data.length.each.with_index(1) do |element, index| %>
  ...
<% end %>

Hope that helps!