Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ruby中迭代器的索引计数方法?_Ruby - Fatal编程技术网

ruby中迭代器的索引计数方法?

ruby中迭代器的索引计数方法?,ruby,Ruby,在迭代过程中移动时,例如: array.each do |row| puts "Current row count: " + row.current_row_index # do some more stuff end 有没有办法获取当前迭代/行的索引?很明显,我可以直接输入一个计数器,但我很好奇是否有一个索引函数的快捷方式来显示它的当前位置 我一直在通过pry挖掘可用的方法,但是我还没有看到任何东西能够做到这一点。如果您需要确切的索引 array.each_with_index |r

在迭代过程中移动时,例如:

array.each do |row|
  puts "Current row count: " + row.current_row_index
  # do some more stuff
end
有没有办法获取当前迭代/行的索引?很明显,我可以直接输入一个计数器,但我很好奇是否有一个索引函数的快捷方式来显示它的当前位置


我一直在通过
pry
挖掘可用的方法,但是我还没有看到任何东西能够做到这一点。

如果您需要确切的索引

array.each_with_index |row, index|
  puts index
end
array.each_index do |index|
  puts index
end

可能是重复的感谢,但下面的答案都有点详细,并提供额外的见解。此外,我认为这个问题也更详细/更具解释性。array.each_with_index do | row,index |将索引置于末尾