Class: Hash

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

Constant Summary

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, &MERGER) : v2
end

Instance Method Summary collapse

Instance Method Details

#deep_merge!(data) ⇒ Object



25
26
27
# File 'lib/i18n/core_ext/hash.rb', line 25

def deep_merge!(data)
  merge!(data, &MERGER)
end

#deep_symbolize_keysObject



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

def deep_symbolize_keys
  inject({}) { |result, (key, value)|
    value = value.deep_symbolize_keys if value.is_a?(Hash)
    result[(key.to_sym rescue key) || key] = value
    result
  }
end

#except(*less_keys) ⇒ Object



8
9
10
# File 'lib/i18n/core_ext/hash.rb', line 8

def except(*less_keys)
  slice(*keys - less_keys)
end

#slice(*keep_keys) ⇒ Object



2
3
4
5
6
# File 'lib/i18n/core_ext/hash.rb', line 2

def slice(*keep_keys)
  h = {}
  keep_keys.each { |key| h[key] = fetch(key) }
  h
end