Class: Hash

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

Direct Known Subclasses

PostData

Instance Method Summary collapse

Instance Method Details

#stringify_keysObject

Return a new hash with all keys converted to strings.



4
5
6
7
8
9
# File 'lib/core_ext/hash_ext.rb', line 4

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

#stringify_keys!Object

Destructively convert all keys to strings.



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

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

#symbolize_keysObject Also known as: to_options

Return a new hash with all keys converted to symbols.



20
21
22
23
24
25
# File 'lib/core_ext/hash_ext.rb', line 20

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

#symbolize_keys!Object Also known as: to_options!

Destructively convert all keys to symbols.



28
29
30
# File 'lib/core_ext/hash_ext.rb', line 28

def symbolize_keys!
  self.replace(self.symbolize_keys)
end