If you like to know Index of each iteration you could use .each_with_index
hash.each_with_index { |(key,value),index| ... }
You could iterate over the keys, and get the values out manually:
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
EDIT: per rampion's comment, I also just learned you can get both the key and value as a tuple if you iterate over hash
:
hash.each_with_index do |(key, value), index|
print "key: #{key}, value: #{value}, index: #{index}\n"
# use key, value and index as desired
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With