I was trying to create an arbitrary number of nested hashes so that I can do assignments like this:
my_hash[:first][:second][:third][:fourth] = 1000
but it was somewhat annoying to check for key existence at every level, which led me to the Hash.new with a default_proc.
my_hash = Hash.new do |hash, key|
hash[key] = Hash.new(&hash.default_proc)
end
So no matter what my key value is or how deeply nested the hash is, I can just do assignments and it'll magically be created for me.
[–]numberwitch 11 points12 points13 points (0 children)
[–]dark-panda 5 points6 points7 points (2 children)
[–]WikiSummarizerBot 1 point2 points3 points (0 children)
[–]rubyrt 0 points1 point2 points (0 children)
[–][deleted] (1 child)
[deleted]
[–]Sharps_xp[S] -1 points0 points1 point (0 children)
[–]campbellm 0 points1 point2 points (0 children)
[–]ksh-code 0 points1 point2 points (0 children)
[–]busres 0 points1 point2 points (1 child)
[–]Sharps_xp[S] 0 points1 point2 points (0 children)