Try:
require 'pp' ary_of_hashes = [{:foo => 1, :bar => 2}, {:foo => 2, :bar => 4}] [3, 6].zip(ary_of_hashes).each do |i, h| h[:baz] = i end pp ary_of_hashes
Result:
[{:foo=>1, :bar=>2, :baz=>3}, {:foo=>2, :bar=>4, :baz=>6}]
zip is a good tool for this, but map will not buy much, at least nothing that you could not do so easily with each in this case.
Also, don't name variables with CamelCase, like myArrayOfHashes , use snake_case like ary_of_hashes . We use CamelCase for class names. Technically, we can use the mixed case for variables, but by convention we do not.
And it is possible to use each_with_index , but this leads to inconvenient code, because it forces you to use the index in [3, 6] . Let zip join the corresponding elements of both arrays and you will have everything you need to massage the hash.
source share