Sha256: aaa4a1f1e4805b76e1c2f8f7fae1ac9564e53a2f1697079d836558e323f58b69

Contents?: true

Size: 982 Bytes

Versions: 3

Compression:

Stored size: 982 Bytes

Contents

class Hash
  def slice(*keep_keys)
    h = {}
    keep_keys.each { |key| h[key] = fetch(key) if key?(key) }
    h
  end unless Hash.method_defined?(:slice)

  def except(*less_keys)
    slice(*keys - less_keys)
  end unless Hash.method_defined?(:except)

  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

  def deep_symbolize_keys!
    replace(deep_symbolize_keys)
  end

  # 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 Hash.method_defined?(:deep_merge)
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
hashr-1.0.0 lib/hashr/core_ext/ruby/hash.rb
hashr-0.0.22 lib/hashr/core_ext/ruby/hash.rb
hashr-0.0.21 lib/hashr/core_ext/ruby/hash.rb