In Ruby, is there a way to use something like hash.each_with_index do |[k,v], i|?

Solution 1:

In fact, yes! Use parentheses:

h = {:a => 1, :b => 2.2}
h.each_with_index do |(k, v), i|
  p k, v, i
end

Solution 2:

The Inject call should get what you want, http://www.ruby-doc.org/core/classes/Enumerable.src/M001494.html check that and scroll to the Inject portion, should work like a charm!