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
257 258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/gemmy/patches/hash_patch.rb', line 257 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 |