Module: Gemmy::Patches::HashPatch::InstanceMethods::Bury
- Defined in:
- lib/gemmy/patches/hash_patch.rb
Overview
The opposite of Hash#dig Takes a list of keys followed by a value to set
Example:
a = {a: {b: {}} }
a.bury(:a, :b, :c, 0)
puts a[:a][:b][:c]
=> 0
Class Method Summary collapse
-
._bury(caller_hash, *args) ⇒ Object
The bury method, taking the input hash as a parameter.
Instance Method Summary collapse
Class Method Details
._bury(caller_hash, *args) ⇒ Object
The bury method, taking the input hash as a parameter
211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/gemmy/patches/hash_patch.rb', line 211 def self._bury(caller_hash, *args) if args.count < 2 raise ArgumentError.new("2 or more arguments required") elsif args.count == 2 caller_hash[args[0]] = args[1] else arg = args.shift caller_hash[arg] = {} unless caller_hash[arg] _bury(caller_hash[arg], *args) unless args.empty? end caller_hash end |