我可能遗漏了一些明显的东西,但是有没有办法访问hash each循环中迭代的索引/计数?
hash = {'three' => 'one', 'four' => 'two', 'one' => 'three'}
hash.each { |key, value|
# any way to know which iteration this is
# (without having to create a count variable)?
}
发布于 2010-01-18 10:38:50
如果你想知道每次迭代的索引,你可以使用.each_with_index
hash.each_with_index { |(key,value),index| ... }
https://stackoverflow.com/questions/2083570
复制相似问题