Class: Hash

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

Instance Method Summary collapse

Instance Method Details

#recursive_symbolize_keys!Hash

Recursively Symbolize Keys!

Returns:



48
49
50
51
52
53
# File 'lib/core_ext/hash.rb', line 48

def recursive_symbolize_keys!
  symbolize_keys!
  values.each{|h| h.recursive_symbolize_keys! if h.is_a?(Hash) }
  values.select{|v| v.is_a?(Array) }.flatten.each{|h| h.recursive_symbolize_keys! if h.is_a?(Hash) }
  self
end

#symbolize_keysHash

Symbolize Keys

Returns:



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

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

#symbolize_keys!Hash

Symbolize Keys!

Returns:



37
38
39
# File 'lib/core_ext/hash.rb', line 37

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

#to_query(namespace = nil) ⇒ String

To query

Parameters:

  • (String)

Returns:

  • (String)


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

def to_query(namespace = nil)
  collect do |key, value|
    value.to_query(namespace ? "#{namespace}[#{key}]" : key)
  end.sort * '&'
end