Class: Hash

Inherits:
Object show all
Defined in:
lib/nobiru/extensions/hash_extension.rb

Instance Method Summary collapse

Instance Method Details

#except(*args) ⇒ Object



34
35
36
37
38
# File 'lib/nobiru/extensions/hash_extension.rb', line 34

def except(*args)
  hash = dup
  args.each {|k| hash.delete(k) }
  hash
end

#only(*args) ⇒ Object



42
43
44
45
46
# File 'lib/nobiru/extensions/hash_extension.rb', line 42

def only(*args)
  hash = {}
  args.each {|k| hash[k] = self[k] if self.has_key?(k) }
  hash
end

#rename_keys(*args) ⇒ Object



49
50
51
52
53
# File 'lib/nobiru/extensions/hash_extension.rb', line 49

def rename_keys(*args)
  keys = Hash[*args.flatten]
  keys.each { |k, v| self[v] = delete(k) if self[k] }
  self
end

#rename_keys!(*args) ⇒ Object



55
56
57
# File 'lib/nobiru/extensions/hash_extension.rb', line 55

def rename_keys!(*args)
  replace(rename_keys(*args))
end

#stringify_keysObject



4
5
6
7
8
9
# File 'lib/nobiru/extensions/hash_extension.rb', line 4

def stringify_keys
  inject({}) do |options, (key, value)|
    options[key.to_s] = value
    options
  end
end

#stringify_keys!Object



13
14
15
# File 'lib/nobiru/extensions/hash_extension.rb', line 13

def stringify_keys!
  replace(stringify_keys)
end

#symbolize_and_underscore_keysObject



59
60
61
62
63
64
# File 'lib/nobiru/extensions/hash_extension.rb', line 59

def symbolize_and_underscore_keys
  inject({}) do |options, (key, value)|
    options[(key.to_s.underscore.to_sym rescue key) || key] = value
    options
  end
end

#symbolize_and_underscore_keys!Object



66
67
68
# File 'lib/nobiru/extensions/hash_extension.rb', line 66

def symbolize_and_underscore_keys!
  replace(symbolize_and_underscore_keys)
end

#symbolize_keysObject



19
20
21
22
23
24
# File 'lib/nobiru/extensions/hash_extension.rb', line 19

def symbolize_keys
  inject({}) do |options, (key, value)|
    options[(key.to_sym rescue key) || key] = value
    options
  end
end

#symbolize_keys!Object



28
29
30
# File 'lib/nobiru/extensions/hash_extension.rb', line 28

def symbolize_keys!
  replace(symbolize_keys)
end