The goal is to start with ['a','b','c'] and end up with {'a'=>{'b'=>{'c'=>{}}}}
so, getting my bearings, i did this:
['a','b','c'].inject({}){|h,v| h.update(v => {})}
# => {"a"=>{}, "b"=>{}, "c"=>{}}
and then figured, if i actually pass on the result hash, it will recurse and nest, but:
['a','b','c'].inject({}){|h,v| h.update(v => {}); h[v]}
# => {}
Why is this? Any idea how to achieve the desired result in an elegant one-liner?
Or in other words:
Construct a hash with the key being the first array element (‘c’ after it is reversed), and the seed (an empty hash) as the value. At each step construct a new hash where the key is the next value from the array, and the value is the hash returned from the previous step.
Here are the results after each iteration of inject:
r={}, v='c'returns{'c'=>{}}r={'c'=>{}}, v='b'returns{'b'=>{'c'=>{}}}r={'b'=>{'c'=>{}}}, v='a'returns{'a'=>{'b'=>{'c'=>{}}}}