Method: Marty::DataExporter.hash_array_merge
- Defined in:
- lib/marty/data_exporter.rb
.hash_array_merge(hl, transpose) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/marty/data_exporter.rb', line 7 def self.hash_array_merge(hl, transpose) # given a list of hashes hl, generates a merged hash. The # resulting hash contains a superset of all the hash keys. The # values are corresponding values from each hash in hl. # e.g. the following # # [{"a"=>1, "b"=>2}, {"a"=>11, "c"=>33}, {"a"=>1111, "b"=>222, "c"=>333}] # # maps to ... # # [["a", "b", "c"], [1, 2, nil], [11, nil, 33], [1111, 222, 333]] keys = hash_array_keys(hl) return keys.each_with_object({}) do |k, rh| rh[k] = hl.map { |h| h[k] } end if transpose [keys.to_a] + hl.map { |h| keys.map { |k| h[k] } } end |