recursion - Ruby dot notation to nested hash keys -
what's best way of converting dot notation path (or array of strings) nested hash key-value? ex: need convert 'foo.bar.baz' equal 'qux' this:
{ 'foo' => { 'bar' => { 'baz' => 'qux' } } } i've done in php, managed creating key in array , setting tmp variable array key's value reference changes take place in array.
try this
f = "root/sub-1/sub-2/file" f.split("/").reverse.inject{|a,n| {n=>a}} #=>{"root"=>{"sub-1"=>{"sub-2"=>"file"}}}
Comments
Post a Comment