I have a nested hash of symbols like this:
{
a: {
b: :c,
d: {
e: :f
}
},
g: :h
}
I want to build an array that contains all symbols used in the hash, both keys and values, in any order. For the example above:
[:a, :b, :c, :d, :e, :f, :g, :h]
Is there any simple, fast and ruby-friendly way to do this?
I'm doing this with a recursive function that sums hash.keys
with hash.values
, and finally applies flatten
to the result.
def all_keys(hash)
hash.keys + hash.values.map { |e| e.is_a?(Hash) ? all_keys(e) : e }
end
h
is your hash, a quick and dirty would beh.to_s.scan /:[a-z]\w*/ => [":a", ":b", ":c", ":d", ":e", ":f", ":g", ":h"]
. \$\endgroup\$ – Cary Swoveland Feb 22 '16 at 19:26