Class: Triglav::Agent::HashUtil
- Inherits:
-
Object
- Object
- Triglav::Agent::HashUtil
- Defined in:
- lib/triglav/agent/hash_util.rb
Class Method Summary collapse
- .deep_stringify_keys(obj) ⇒ Object
- .deep_symbolize_keys(obj) ⇒ Object
-
.setdig(hash, key, val) ⇒ Object
HashUtil.setdig(hash, ['a', 'b'], 'bar') # like hash['b'] = 'bar'.
Class Method Details
.deep_stringify_keys(obj) ⇒ Object
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/triglav/agent/hash_util.rb', line 14 def self.deep_stringify_keys(obj) case obj when Hash obj.map {|k, v| [k.to_s, deep_stringify_keys(v)] }.to_h when Array obj.map {|v| deep_stringify_keys(v) } else obj end end |
.deep_symbolize_keys(obj) ⇒ Object
3 4 5 6 7 8 9 10 11 12 |
# File 'lib/triglav/agent/hash_util.rb', line 3 def self.deep_symbolize_keys(obj) case obj when Hash obj.map {|k, v| [k.to_sym, deep_symbolize_keys(v)] }.to_h when Array obj.map {|v| deep_symbolize_keys(v) } else obj end end |
.setdig(hash, key, val) ⇒ Object
HashUtil.setdig(hash, ['a', 'b'], 'bar') # like hash['b'] = 'bar'
26 27 28 29 30 31 32 33 34 |
# File 'lib/triglav/agent/hash_util.rb', line 26 def self.setdig(hash, key, val) keys = Array(key) sub_hash = hash keys[0...-1].each do |k| sub_hash = (sub_hash[k] ||= {}) end sub_hash[keys.last] = val hash end |