Sha256: 8dbf404f51cf8645e8f6fdc04dc2832aa4c2c645da0b79b46363fc454f4268e0

Contents?: true

Size: 731 Bytes

Versions: 4

Compression:

Stored size: 731 Bytes

Contents

class Hash
  def deep_symbolize_keys
     symbolizer = lambda do |value|
      case value
      when Hash
        value.deep_symbolize_keys
      when Array
        value.map { |value| symbolizer.call(value) }
      else
        value
      end
    end
    inject({}) do |result, (key, value)|
      result[(key.to_sym rescue key) || key] = symbolizer.call(value)
      result
    end
  end unless instance_methods.include?(:deep_symbolize_keys)

  # deep_merge_hash! by Stefan Rusterholz, see http://www.ruby-forum.com/topic/142809
  def deep_merge(other)
    merger = proc { |key, v1, v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : v2 }
    merge(other, &merger)
  end unless instance_methods.include?(:deep_merge)
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
hashr-2.0.1 lib/hashr/core_ext/ruby/hash.rb
hashr-2.0.0 lib/hashr/core_ext/ruby/hash.rb
hashr-2.0.0.rc2 lib/hashr/core_ext/ruby/hash.rb
hashr-2.0.0.rc1 lib/hashr/core_ext/ruby/hash.rb