Class: Hash

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

Instance Method Summary collapse

Instance Method Details

#deep_merge(other_hash) ⇒ Object

Returns a new hash with self and other_hash merged recursively.


4
5
6
# File 'lib/texas/core_ext/hash.rb', line 4

def deep_merge(other_hash)
  dup.deep_merge!(other_hash)
end

#deep_merge!(other_hash) ⇒ Object

Returns a new hash with self and other_hash merged recursively. Modifies the receiver in place.


11
12
13
14
15
16
17
# File 'lib/texas/core_ext/hash.rb', line 11

def deep_merge!(other_hash)
  other_hash.each_pair do |k,v|
    tv = self[k]
    self[k] = tv.is_a?(Hash) && v.is_a?(Hash) ? tv.deep_merge(v) : v
  end
  self
end

#stringify_keysObject

Destructively convert all keys to strings.


21
22
23
# File 'lib/texas/core_ext/hash.rb', line 21

def stringify_keys
  dup.stringify_keys!
end

#stringify_keys!Object

Destructively convert all keys to strings. Modifies the receiver in place.


28
29
30
31
32
33
# File 'lib/texas/core_ext/hash.rb', line 28

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