Ruby: Get all keys in a hash (including sub keys)
hash, ruby
Solution
This will give you an array of all the keys for any level of nesting.
def get_em(h)
h.each_with_object([]) do |(k,v),keys|
keys << k
keys.concat(get_em(v)) if v.is_a? Hash
end
end
hash = {"a" => 1, "b" => {"c" => {"d" => 3}}}
get_em(hash) # => ["a", "b", "c", "d"]
Problem
let's have this hash: ``` hash = {"a" => 1, "b" => {"c" => 3}} hash.get_all_keys => ["a", "b", "c"] ``` how can i get all keys since `hash.keys` returns just `["a", "b"]`