Module: HashOp::Merge
- Defined in:
- lib/hash_op/merge.rb
Class Method Summary collapse
-
.by_group(hashes, key) ⇒ Object
Merge hashes by grouping them on the specified key value and merging them all together.
-
.flat(hashes) ⇒ Object
Merge all specified hashes by merging the second in the first, the third in the result, and so on.
Class Method Details
.by_group(hashes, key) ⇒ Object
Merge hashes by grouping them on the specified key value and merging them all together.
15 16 17 18 |
# File 'lib/hash_op/merge.rb', line 15 def by_group(hashes, key) groups = hashes.group_by { |h| h[key] } groups.values.map { |g| flat(g) } end |
.flat(hashes) ⇒ Object
Merge all specified hashes by merging the second in the first, the third in the result, and so on.
6 7 8 9 10 |
# File 'lib/hash_op/merge.rb', line 6 def flat(hashes) hashes.inject({}) do |result, hash| result.merge hash end end |