Sha256: 57f55768a25f1026be29a27d096ff4cf4b1560871afc804f0cb8e576566a1be2

Contents?: true

Size: 429 Bytes

Versions: 1

Compression:

Stored size: 429 Bytes

Contents

require "hash_flatten/version"

module HashFlatten
  refine Hash do
    def flatten
      flattened = each_with_object({}) do |(k, v), n|
        if v.is_a? Hash
          v.each do |k2, v2|
            n["#{k}.#{k2}"] = v2
          end
        else
          n[k.to_s] = v
        end
      end

      if flattened.any? { |_, v| v.is_a? Hash }
        flattened.flatten
      else
        flattened
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hash_flatten-0.1.0 lib/hash_flatten.rb