Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/core_ext/hash/compact.rb,
lib/core_ext/hash/deep_symbolize_keys.rb

Instance Method Summary collapse

Instance Method Details

#compactObject



2
3
4
# File 'lib/core_ext/hash/compact.rb', line 2

def compact
  dup.compact!
end

#compact!Object



6
7
8
9
10
11
# File 'lib/core_ext/hash/compact.rb', line 6

def compact!
  keys.each do |key|
    delete(key) if self[key].nil?
  end
  self
end

#deep_symbolize_keysObject



2
3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/core_ext/hash/deep_symbolize_keys.rb', line 2

def deep_symbolize_keys
  inject({}) { |result, (key, value)|
    result[(key.to_sym rescue key) || key] = case value
    when Array
      value.map { |value| value.is_a?(Hash) ? value.deep_symbolize_keys : value }
    when Hash
      value.deep_symbolize_keys
    else
      value
    end
    result
  }
end