The double hash retrieval is summarized as an output.
The logical thinking test was so devastating that I couldn't even see it, so I felt a sense of crisis and decided to leave it in my thoughts and output.
As a beginner, I would appreciate it if you could give me any suggestions or advice.
There are multiple hashes in the array as shown below.
He wants to see where it's OK for dinner to make his first date a success.
ruby.rb
first_date = [
{ dinner: { OK: "Italian"}},
{ dinner: { OK: "Quiet_izakaya"}},
{ dinner: { NG: "Gyudon"}}
]
Retrieving a specific value from a hash
ruby.rb
first_date[:dinner][:OK]
Isn't it?
But unfortunately this time there is a ** hash in the ** array.
Therefore, you need to use each to retrieve the elements from the array.
ruby.rb
first_date.each do |d|
puts d [:dinner][:OK]
end
#result
Italian
Quiet_izakaya
Yes!
It seems to be okay if it's Italian or a quiet izakaya!
Let's stop beef bowl for dinner on the first date.
By the way, even if the hash has a multiple structure, you only need to increase the number of specified keys.
ruby.rb
real = [
{ gohan: { dinner: { OK: "MATSUYA"}}},
{ gohan: { dinner: { OK: "OOTOYA"}}},
{ gohan: { dinner: { OK: "TENYA"}}}
]
real.each do |d|
puts d [:gohan][:dinner][:OK]
end
#result
MATSUYA
OOYOYA
TENYA
Yes! Anything seems OK!
Recommended Posts