ruby.rb
user_data = [
{
user: {
profile: {
name: 'takeshi'
}
}
},
{
user: {
profile: {
name: 'tanaka'
}
}
},
{
user: {
profile: {
name: 'tosiki'
}
}
}
]
In the above code, the individual data exists in the array as a double hash. __ ** I want to put all the user names in order! !! !! ** __
ruby.rb
#each method
Unused patterns
puts user_date[0]["user"]["profile"]["name"]
puts user_date[1]["user"]["profile"]["name"]
puts user_date[2]["user"]["profile"]["name"]
ruby.rb
#each method usage pattern
user_date.each do |date|
puts date [:user][:profile][:name]
end
When multiple hashes overlap, the value of the name key can be displayed by specifying the key that you want to display consecutively from the first "key" of the hash.
__ I was able to output just the name brilliantly! !! __
???????????@MacBook-Pro ruby % ruby posi.rb
takeshi
tanaka
tosiki
It may be because I'm a beginner that it's hard to see if it's the top code description method, but I converted it to the following code description method ...
user_date = [
{ user: { profile: { name: "takeshi" }}},
{ user: { profile: { name: "tanaka" }}},
{ user: { profile: { name: "tosiki" }}}
]
Recommended Posts