Sha256: 1bb5cda7858680efc74825fb857e0acba30603575f26070621d9ba47afbb591b

Contents?: true

Size: 908 Bytes

Versions: 2

Compression:

Stored size: 908 Bytes

Contents

Hash.class_eval do
  # Merges self with another hash, recursively.
  #
  # This code was lovingly stolen from some random gem:
  # http://gemjack.com/gems/tartan-0.1.1/classes/Hash.html
  #
  # Thanks to whoever made it.
  def deep_merge(hash)
    target = dup

    hash.keys.each do |key|
      if hash[key].is_a? Hash and self[key].is_a? Hash
        target[key] = target[key].deep_merge(hash[key])
        next
      end

      target[key] = hash[key]
    end

    target
  end
  
  def deep_merge!(second)
    second.each_pair do |k,v|
      if self[k].is_a?(Hash) and second[k].is_a?(Hash)
        self[k].deep_merge!(second[k])
      else
        self[k] = second[k]
      end
    end
  end

  def deep_key?(key)
    exist = false

    keys.each do |k|
      if self[k].is_a? Hash
        exist = self[k].deep_key? key
        break
      end

      exist = (k == key)
    end
    
    exist
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
trinidad-0.7.1 lib/trinidad/core_ext.rb
trinidad-0.7.0 lib/trinidad/core_ext.rb