I have the following hash:
hash = {\'name\' => { \'Mike\' => { \'age\' => 10, \'gender\' => \'m\' } } }
I can access the age by:
If your goal is to raise a KeyError
when any of the intermediate keys are missing, then you need to write your own method. If instead you're using fetch to provide default values for missing keys, then you can circumvent the use of fetch by constructing the Hashes with a default values.
hash = Hash.new { |h1, k1| h1[k1] = Hash.new { |h2, k2| h2[k2] = Hash.new { |h3, k3| } } }
hash['name']['Mike']
# {}
hash['name']['Steve']['age'] = 20
hash
# {"name"=>{"Mike"=>{}, "Steve"=>{"age"=>20}}}
This won't work for arbitrarily nested Hashes, you need to choose the maximum depth when you construct them.