This question is the inverse of this question.
Given a nested hash like
{
:a => {
:b => {:c => 1, :d => 2},
:e => 3,
},
:f => 4,
}
what is the best way to convert it into a flat hash like
{
[:a, :b, :c] => 1,
[:a, :b, :d] => 2,
[:a, :e] => 3,
[:f] => 4,
}
A declarative solution using DeepEnumerable:
or, for those who prefer point-free style
Array support / readable names / no update for speed / stringified results keys
Very similar to Adiel Mittmann's solution
Edit: Refactored for elegance. Should be almost as fast.
Inspired by @cary-swoveland way, but in Hash class :
My attempt:
Sorry for the bad variables names, had to fit it in one line.
This is not an attempt to give you the best way to do it, but it is a way :P
It works for your example, producing this result:
It may not produce the result you expect if there are empty hashes.