I have a hash:
a = {
"person" => [
{
"age" => ["0"],
"details" => [
{
"hobby" => ["6"],
"interest" => ["programming"]
}
]
}
]
}
I would like to get the value of the “details”. The easy way to do it will be
a["person"].first["details"].first
But, it is too long and doesn’t look good. Is there any other way I can do this?
If your search conditions based on structure of hash, the easiest way to do it will be represent this structure in access command what you have done.
If you want several chars less, here it is:
If your search conditions based on key names, you can create your own function in Hash class to search over nested hashes and do something like this:
If you don’t like square brackets, use https://github.com/intridea/hashie and access data in hash through attributes:
Anyway, you can do nothing, because your code looks pretty much usual for any rubyist.