Finding out current index in EACH loop (Ruby)
Possible Duplicate:
Automatic counter in Ruby for each?
我想在每个循环中都找到当前索引。我该怎么办?
1 2 3 4 5
| X=[1,2,3]
X.each do |p|
puts"current index..."
end |
- 复制了stackoverflow.com/questions/533837/…
1 2 3
| X.each_with_index do |item, index|
puts"current_index: #{index}"
end |
-
这个解决方案比重复的解决方案更清楚,更简洁。
-
您实际上可以在for循环for each,index in X.each_with_index; ...; end中执行此操作
-
为什么ruby的作者烦恼地介绍each_with_index ...
x.each_with_index { |v, i| puts"current index...#{i}" }
- i应该是该块的第二个参数
-
丘巴斯,是的,总能让我受益,谢谢
-
没有each_with_index方法的情况下如何获取索引值?