Traverse mix of hashes and arrays in Ruby
If you have a hash or array with nested hashes and arrays, you can use dig
and pass as an argument a mix of keys and indexes:
response = {
data: {
locations: [
{
name: "Jacksonville"
}
]
}
}
response.dig(:data, :locations, 0, :name)
=> "Jacksonville"
Tweet