module Gemmy::Patches::HashPatch::InstanceMethods::Bury

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

Source: github.com/dam13n/ruby-bury/blob/master/hash.rb

Public Class Methods

bury(caller_hash, *args) click to toggle source

The bury method, taking the input hash as a parameter

# 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

Public Instance Methods

bury(*args) click to toggle source
# File lib/gemmy/patches/hash_patch.rb, line 253
def bury *args
  Gemmy.patch("hash/i/bury").bury self, *args
end