Sha256: c947c5946746c199da570c21353a6e6e69c55d614faa9b7b09678cfdd1a60355

Contents?: true

Size: 539 Bytes

Versions: 2

Compression:

Stored size: 539 Bytes

Contents

module HashOp
  module Merge

    # Merge all specified hashes by merging the second
    # in the first, the third in the result, and so on.
    def flat(hashes)
      hashes.inject({}) do |result, hash|
        result.merge hash
      end
    end
    module_function :flat

    # Merge hashes by grouping them on the
    # specified key value and merging them all together.
    def by_group(hashes, key)
      groups = hashes.group_by { |h| h[key] }
      groups.values.map { |g| flat(g) }
    end
    module_function :by_group
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
hash_op-0.3.0 lib/hash_op/merge.rb
hash_op-0.2.0 lib/hash_op/merge.rb