gouf
8/22/2014 - 10:48 PM

Ref : http://blog.arkency.com/2014/07/hidden-features-of-ruby-you-may-dont-know-about/

# define nested_hash
nested_hash = Hash.new { |hash, key| hash[key] = Hash.new(&hash.default_proc) }
# and clone to use
n = nested_hash.clone

# storing data
n[:x][:y][:z] = :xyz
n[:x][:a] = :abc
n[:x][:y][:a] = :cdf
n[:a][:n][:a] = :cdf
n[:b][:y][:a] = :cfd
n[:b][:n][:a] = :cfd
n[:b][:n][:a] = :cfd

p nested_hash #=> {}
pp n
=begin
{:x=>{:y=>{:z=>:xyz, :a=>:cdf}, :a=>:abc},
 :a=>{:n=>{:a=>:cdf}},
 :b=>{:y=>{:a=>:cfd}, :n=>{:a=>:cfd}}}
=end