Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/spice/core_ext/hash.rb

Direct Known Subclasses

Mash, Spice::IdentityMap

Instance Method Summary collapse

Instance Method Details

#deep_merge(hash) ⇒ Hash

Merges self with another hash, recursively

Parameters:

  • hash (Hash)

    The hash to merge

Returns:


24
25
26
27
28
29
30
31
32
33
34
# File 'lib/spice/core_ext/hash.rb', line 24

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

#except(*keys) ⇒ Hash

Return a hash that includes everything but the given keys.

Parameters:

Returns:


7
8
9
# File 'lib/spice/core_ext/hash.rb', line 7

def except(*keys)
  self.dup.except!(*keys)
end

#except!(*keys) ⇒ Hash

Replaces the hash without the given keys.

Parameters:

Returns:


15
16
17
18
# File 'lib/spice/core_ext/hash.rb', line 15

def except!(*keys)
  keys.each{|key| delete(key)}
  self
end

#stringify_keys!Object


36
37
38
39
40
41
# File 'lib/spice/core_ext/hash.rb', line 36

def stringify_keys!
  keys.each do |key|
    self[key.to_s] = delete(key)
  end
  self
end

#symbolize_keys!Object


43
44
45
46
47
48
# File 'lib/spice/core_ext/hash.rb', line 43

def symbolize_keys!
  keys.each do |key|
    self[(key.to_sym rescue key) || key] = delete(key)
  end
  self
end