I will summarize how to retrieve the double hash as a memorandum.
Ruby 2.6.5
For example, suppose you have a variable user_date with multiple hashes inside an array.
user_data = [
{
user: {
profile: {
name: 'Taro'
}
}
},
{
user: {
profile: {
name: 'Jiro'
}
}
},
{
user: {
profile: {
name: 'Saburo'
}
}
}
]
To get the value of the hash, specify the key corresponding to that value.
hash[Key of the value to get]
In addition, when retrieving specific data from a double hash, specify the [Key of the value to be retrieved] continuously up to the data to be retrieved. Therefore. If you try to output only the names of all users, the description will be as follows.
user_data.each do |user|
puts user[:user][:profile][:name]
end
that's all
Recommended Posts