module Gemmy::Patches::HashPatch::InstanceMethods::Autovivified

Turns a hash into one that's “autovivified” meaning it's default values for keys is an empty hash. The result is that you can set nested keys without initializing more than one hash layer.

Usage:

hash = {}.autovivified
hash[:a][:b] = 0
puts hash[:a][:b]
=> 0

Public Class Methods

_autovivified(caller_hash) click to toggle source
# File lib/gemmy/patches/hash_patch.rb, line 283
def self._autovivified(caller_hash)
  result = Hash.new do |hash,key|
    hash[key] = Hash.new(&hash.default_proc)
  end
  result.deep_merge caller_hash
end

Public Instance Methods

autovivified() click to toggle source
# File lib/gemmy/patches/hash_patch.rb, line 289
def autovivified
  Gemmy::Patches::HashPatch::InstanceMethods::Autovivified._autovivified(self)
end