This is kinda of confusing.
If you have a hash that contains more hashes that also have hashes and so on, how do you determine if a member exists that is more than one layer deep?
For example:
hash 1 =
{
"layer1" =>
{
"layer2" =>
{
"layer3" => ['Something', 'Array']
}
}
}
How would you go about verifying 'Something' exists in the above hash if the hash only has:
hash2 =
{
"layer1" =>
{
"layer2" => ['Other Array']
}
}
For example, I would try to do:
if hash2['layer1']['layer2']['layer3'].contains? 'Something'
puts "Found Something!"
end
But this would error undefined method `contains?' for nil:NilClass. Where layer3 would be the NilClass because it doesn't exist. It would suffice if one of these embedded hashses was Nil then say it doesn't exist, but you can't easily test for their existence because that also will return Nil if you are a layer too deep. Is there a function in ruby that checks each top level layer for Nil recursively instead of the specific member you request when you would call .nil? E.g. What I would think would work!
if hash2['layer1']['layer2']['layer3'].nil?
puts 'layer3 exists'
end
But .nil? only checks if 'layer3' exists. IS there a method that starts at 'layer1' then checks if 'layer2' exists then 'layer3' and so forth. And at any of the parts are nil it returns false? Because if 'layer2' or 'layer1' didn't exist it would error out saying undefined method `[]' for nil:NilClass.