In order to improve my Rails
app security, I want to know how to get all keys from a parametters hash
hash example :
params =
{"id"=>23, "name"=>"test", "infos"=>{"data"=>"er323e3ee23reQir", "signed"=>true}}
if I do params.keys
I obtained : ["id", "name", "infos"]
But I want to get ALL keys, in a simple array, like : ["id", "name", "infos", "data", "signed"]
there is a simple way to do this ?
Try this:
def deep_hash_keys(h)
h.keys + h.map { |_, v| v.is_a?(Hash) ? deep_hash_keys(v) : nil }.flatten.compact
end
params = {"id"=>23, "name"=>"test", "infos"=>{"data"=>"er323e3ee23reQir", "signed"=>true}}
deep_hash_keys(params) # => ["id", "name", "infos", "data", "signed"]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With