Array of hashes to hash

arrays, hash, ruby

Solution

You may use

a.reduce Hash.new, :merge

which directly yields

{:a=>:b, :c=>:d}

Note that in case of collisions the order is important. Latter hashes override previous mappings, see e.g.:

[{a: :b}, {c: :d}, {e: :f, a: :g}].reduce Hash.new, :merge   # {:a=>:g, :c=>:d, :e=>:f}

Problem

For example, I have array of single hashes ``` a = [{a: :b}, {c: :d}] ``` What is best way to convert it into this? ``` {a: :b, c: :d} ```

Original source