I'd like to flatten nested hash to an array. For example:
a = {'1'=>{'2'=>{'5'=>{},'6'=>{'8'=>{}}}},'3'=>{},'4'=>{'7'=>{}}}
and result of flatten_nested_hash(a) would be:
["1", "2", "5", "6", "8", "3", "4", "7"]
Finally I wrote some recursive function, but I feel that there must be some easier, non-recursive way of doing it.
My function looks like this:
def flatten_nested_hash(categories)
categories.map do |k,v|
if v == {}
k
else
[k,flatten_nested_hash(v)]
end
end.flatten
end