I’m facing I guess a basic issue, but I can’t find a better way to
deal with it.
I have an Array:
a = [ { "address_number" => 123,
"suite"=> 342,
"crazy_hash" => true,
"crazy_hash" => [{"why_hash_in_array" => true}]
},
"I'm a string",
123,
{ "empty" => ""}
]
I want to access the Hash with the key “why_hash_in_array”, and am
doing this:
b = a.first["crazy_hash"].first["why_hash_in_array"]
I understand that this is not a good way to access the data because if
the array changes the order, the retrieval of that key will not work
anymore.
Is there a method from Ruby or Rails that can “magically” retrieve
that keypair? Something like:
a.magic_hash_wand["why_hash_in_array"]
=> true
Here’s a magic chain that will help you 🙂
It’s immune to array reordering and will return first
crazy_hashit sees. If thatcrazy_hashis not a hash, it will raise an error.