From: Joel VanderWerf Date: 2007-01-22T10:46:00+09:00 Subject: Re: Tricky: converting path into a Hash Robert MannI wrote: > Hello! > > > I am wondering if the mighty ruby crowd has a brilliant idea for a tricky > problem I am solving. > > I need to store a path as a tree in a hash. > > Given: > a/b/c > > I want the Hash: > { 'a' => { 'b' => { 'c' => { } } } } > or written differently > hsh['a']['b']['c'] = {} This is a fairly standard idiom: pr = proc {|h,k| h[k] = Hash.new(&pr)} h = Hash.new(&pr) h['a']['b']['c'] = {} p h # ==> {"a"=>{"b"=>{"c"=>{}}}} For more discussion, search for "autovivify hash" on the list... -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407