let's say i have a multidimensional hash, and in one of the subhashes i have a key=>value pair which i need to retrieve by key. how can i do it?
example hashes:
h={:x=>1,:y=>2,:z=>{:a=>{:k=>"needle"}}}
h={:k=>"needle"}
key is always :k, and i need to get "needle"
i noticed that there is no "flatten" function for hashes in ruby 1.8, but if it'd be there, i imagine i'd just do
h.flatten[:k]
i imagine i need to write a recursive function for that?
thanks
If you need simply to fetch key value, but don't know how deep the key is, use this snippet
You can always write your own mission-specific extension to Hash which does the dirty work for you:
You can use this quite simply: