Class: Hash
- Defined in:
- lib/core_ext/hash/compact.rb,
lib/core_ext/hash/deep_merge.rb,
lib/core_ext/hash/deep_symbolize_keys.rb
Constant Summary collapse
- DEEP_MERGER =
deep_merge_hash! by Stefan Rusterholz, see www.ruby-forum.com/topic/142809
proc do |key, v1, v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &DEEP_MERGER) : v2 end
Instance Method Summary collapse
- #compact ⇒ Object
- #deep_merge(data) ⇒ Object
- #deep_merge!(data) ⇒ Object
- #deep_symbolize_keys ⇒ Object
- #deep_symbolize_keys! ⇒ Object
Instance Method Details
#compact ⇒ Object
2 3 4 5 6 7 |
# File 'lib/core_ext/hash/compact.rb', line 2 def compact inject({}) do |result, (key, value)| result[key] = value unless value.nil? result end end |
#deep_merge(data) ⇒ Object
7 8 9 |
# File 'lib/core_ext/hash/deep_merge.rb', line 7 def deep_merge(data) merge(data, &DEEP_MERGER) end |
#deep_merge!(data) ⇒ Object
11 12 13 |
# File 'lib/core_ext/hash/deep_merge.rb', line 11 def deep_merge!(data) merge!(data, &DEEP_MERGER) end |
#deep_symbolize_keys ⇒ Object
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 |
#deep_symbolize_keys! ⇒ Object
16 17 18 |
# File 'lib/core_ext/hash/deep_symbolize_keys.rb', line 16 def deep_symbolize_keys! replace(deep_symbolize_keys) end |