Gary Wright
1/28/2008 8:07:00 PM
On Jan 28, 2008, at 2:51 PM, 7stud -- wrote:
> #Create a hash where looking up a non-existent key
> #in the hash results in the key being created and
> #assigned an empty hash:
> results = Hash.new do |hash, key|
> hash[key] = {}
> end
This works for a two-level hash, which is what the OP needed.
If you want an infinite-level hash:
lazy = lambda { |h,k| h[k] = Hash.new(&lazy) }
h = Hash.new(&lazy)
h[1][2][3][4] = 5
p h # {1=>{2=>{3=>{4=>5}}}}
Gary Wright