Class: Hash

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

Instance Method Summary collapse

Instance Method Details

#reverse_merge!(other = {}) ⇒ Object



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

def reverse_merge!(other = {})
  other.each{|k,v| self[k] ||= v}
  self
end

#slice(*keys) ⇒ Object



18
19
20
21
# File 'lib/any_view/core_ext/hash.rb', line 18

def slice(*keys)
  out = dup
  out.slice!(*keys)
end

#slice!(*keys) ⇒ Object



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

def slice!(*keys)
  keys = keys.flatten
  out = {}
  keys.each{|k| out[k] = self[k]}
  replace out
  self
end

#symbolize_keysObject



25
26
27
28
# File 'lib/any_view/core_ext/hash.rb', line 25

def symbolize_keys
  out = dup
  out.symbolize_keys!
end

#symbolize_keys!Object



32
33
34
35
36
37
38
39
# File 'lib/any_view/core_ext/hash.rb', line 32

def symbolize_keys!
  symbolizable_keys = []
  keys.each{|k| symbolizable_keys << k if k.respond_to?(:to_sym) && !k.is_a?(Symbol)}
  symbolizable_keys.each do |k|
    self[k.to_sym] = delete(k)
  end
  self
end