Reputation: 3816
I am using a ruby iterator on a view in a rails app like so:
<% ([email protected]).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.
Upvotes: 43
Views: 58345
Reputation: 1311
For anyone who came here looking for each_with_index
starting at an offset - You are looking for with_index
.
change each_with_index
to each.with_index(2)
for starting with index 2
Upvotes: 0
Reputation: 9491
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 %>
Upvotes: 157
Reputation: 1068
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
Upvotes: 12
Reputation: 1071
Use Integer#next
:
[:a, :b, :c].each_with_index do |value, index|
puts "value: #{value} has index: #{index.next}"
end
produces:
value: a has index: 1
value: b has index: 2
value: c has index: 3
Upvotes: 10
Reputation: 11
I had the same problem, and solved it by using the each_with_index method. But added 1 to the index in the code.
@someobject.each_with_index do |e, index|
= index+1
Upvotes: 0
Reputation: 16619
If I understand your question right, you want to start the index from 1, but in ruby arrays goes as 0 base indexes, so the simplest way would be
given @document.data
is an array
index = 1
@document.data.each do |element|
#your code
index += 1
end
HTH
Upvotes: 1
Reputation: 16834
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
Upvotes: 37
Reputation: 22258
An array index is always going to be zero based.
If you want to skip the first element, which it sounds like you do:
@document.data[1..-1].each do |data|
...
end
Upvotes: 1
Reputation: 14943
There is no such thing as making the index start from 1. If you want to skip the first item in the array use next
.
<% ([email protected]).each_with_index do |element, index| %>
next if index == 0
<% end %>
Upvotes: 2