Sha256: 13a7df72dad2e3c2a051ecc498455a86bc7ee37d38f28d79c65a9fa1efe8965e

Contents?: true

Size: 1.45 KB

Versions: 37

Compression:

Stored size: 1.45 KB

Contents

class Hash

  # Returns a new hash just like this one, but with all the string keys expressed as symbols.
  # Also applies to hashes within self.
  # Based on an implementation within Rails 2.x, thanks Rails!
  def deep_symbolize
    target = dup    
    target.inject({}) do |memo, (key, value)|
      value = value.deep_symbolize if value.is_a?(Hash)
      memo[(key.to_sym rescue key) || key] = value
      memo
    end
  end
  
  # Returns a new hash just like this one, but with all the symbol keys expressed as strings.
  # Also applies to hashes within self.
  # Based on an implementation within Rails 2.x, thanks Rails!
  def deep_stringify
    target = dup
    target.inject({}) do |memo, (key, value)|
      value = value.deep_stringify if value.is_a?(Hash)
      memo[(key.to_s rescue key) || key] = value
      memo
    end
  end
      
  # 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 to_xml
    inject("") do |memo, (key, value)|
      memo << "<#{key}>#{(value.respond_to?(:to_xml))? value.to_xml : value}</#{key}>"
    end
  end

end

Version data entries

37 entries across 37 versions & 2 rubygems

Version Path
videojuicer-vj-sdk-0.1.12 lib/core_ext/hash.rb
vj-sdk-0.8.2 lib/core_ext/hash.rb
vj-sdk-0.8.0.1 lib/core_ext/hash.rb
vj-sdk-0.7.14 lib/core_ext/hash.rb
vj-sdk-0.7.13 lib/core_ext/hash.rb
vj-sdk-0.7.11 lib/core_ext/hash.rb
vj-sdk-0.7.10 lib/core_ext/hash.rb
vj-sdk-0.7.9 lib/core_ext/hash.rb
vj-sdk-0.7.8 lib/core_ext/hash.rb
vj-sdk-0.7.7 lib/core_ext/hash.rb
vj-sdk-0.7.6 lib/core_ext/hash.rb
vj-sdk-0.7.5 lib/core_ext/hash.rb
vj-sdk-0.7.4 lib/core_ext/hash.rb
vj-sdk-0.7.3 lib/core_ext/hash.rb
vj-sdk-0.7.2 lib/core_ext/hash.rb
vj-sdk-0.7.1 lib/core_ext/hash.rb
vj-sdk-0.7.0 lib/core_ext/hash.rb
vj-sdk-0.6.9 lib/core_ext/hash.rb
vj-sdk-0.6.8 lib/core_ext/hash.rb
vj-sdk-0.6.7 lib/core_ext/hash.rb