Lets say I have a hash which may contain hashes.
params: { :action => "index", :controller => "home",
:secret => "I love Jeff Atwood",
:user => {name => "Steve", secret => "I steal Joel's pants"}}
Is there an elegant way to traverse the hash and remove all the “secret” keys I come across including the subhashes. (The hashes are not restricted so no way to know what the hashes may contain in advance.)
I know I could do
params.delete(:secret)
but that wouldn’t get the secret from the ‘user’ hash.
I don’t think there is a built in method for this so a simple recursive method along the following lines is one solution:
With your example data:
Gives:
Note that this solution works in place i.e. it is modifying the original Hash, not returning a new Hash with the requested key excluded.