ruby each_with_index_do 从 1 开始作为索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14387589/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
each_with_index_do starting at 1 for index
提问by Codejoy
I am using a ruby iterator on a view in a rails app like so:
我在 Rails 应用程序的视图上使用 ruby 迭代器,如下所示:
<% ([email protected]).each_with_index do |element, index| %>
...
<% end %>
I thought the addition of the 1.. instead of just saying:
@document.data
我认为添加 1.. 而不是仅仅说:
@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.
将得到使上面的索引从 1 开始的技巧。但是,上面的代码索引仍然是 0 到 data.length(有效 -1)。那么我做错了什么,我需要索引等于 1-data.length...不知道如何设置迭代器来做到这一点。
回答by Tyler Rick
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:
除非您使用像 1.8 这样的旧 Ruby(我认为这是在 1.9 中添加的,但我不确定),否则您可以使用each.with_index(1)以获取基于 1 的枚举器:
In your case it would be like this:
在你的情况下,它会是这样的:
<% @document.data.length.each.with_index(1) do |element, index| %>
...
<% end %>
Hope that helps!
希望有帮助!
回答by Matthew Rudy
I think maybe you misunderstand each_with_index.
我想也许你误会了each_with_index。
eachwill iterate over elements in an array
each将迭代数组中的元素
[:a, :b, :c].each do |object|
puts object
end
which outputs;
哪些输出;
:a
:b
:c
each_with_indexiterates over the elements, and also passes in the index (starting from zero)
each_with_index迭代元素,并传入索引(从零开始)
[: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.
如果你想要它 1-indexed 那么只需添加 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
回答by abbottjam
Use Integer#next:
使用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
回答by worrawut
This may not be exactly the same each_with_indexmethod in question, but I think the result may close to something in mod is asking...
这可能与所讨论的each_with_index方法不完全相同,但我认为结果可能接近 mod 中要求的某些内容......
%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
有关更多信息https://ruby-doc.org/core-2.6.1/Enumerator.html#method-i-with_index
回答by Hitham S. AlQadheeb
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.
没有让索引从 1 开始这样的事情。如果你想跳过数组中的第一项,请使用next.
<% ([email protected]).each_with_index do |element, index| %>
next if index == 0
<% end %>
回答by Kyle
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
回答by sameera207
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
如果我理解你的问题,你想从 1 开始索引,但在 ruby 数组中作为 0 基本索引,所以最简单的方法是
given @document.datais an array
给定的@document.data是一个数组
index = 1
@document.data.each do |element|
#your code
index += 1
end
HTH
HTH
回答by Eirik Coppa Thommessen
I had the same problem, and solved it by using the each_with_index method. But added 1 to the index in the code.
我遇到了同样的问题,并通过使用 each_with_index 方法解决了它。但是在代码中给索引加了1。
@someobject.each_with_index do |e, index|
= index+1

