我可能遗漏了一些显而易见的东西,但是有没有一种方法可以在 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)?
}
hash.keys.each_with_index do |key, index|
value = hash[key]
print "key: #{key}, value: #{value}, index: #{index}\n"
# use key, value and index as desired
end