[Ruby] Extracting double hash in array

Overview

The double hash retrieval is summarized as an output.

background

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.

Contents

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

[Ruby] Extracting double hash in array
[ruby] Double hash
Ruby double hash
[Ruby] Extracting a two-dimensional array
Multiplication in a Ruby array
[Ruby] Array
Extraction of "ruby" double hash * Review
How to retrieve the hash value in an array in Ruby
Hanachan in Ruby (non-destructive array manipulation)
Ruby Hash notes
Class in Ruby
[Ruby] Hash retrieval
[About double hash]
Ruby two-dimensional array
Heavy in Ruby! ??
Ruby array manipulation
[Ruby] Calculation by extracting elements from an array
Examine the elements in the array using the [Ruby] includes? Method
Calculate the difference between numbers in a Ruby array
Output triangle in Ruby
Variable type in ruby
Fast popcount in Ruby
Multidimensional array in Swift
[Ruby] Count an even number in an array using the even? Method
[Ruby] Searching for elements in an array using binary search
About the difference between "(double quotation)" and "single quotation" in Ruby
[Ruby] How to retrieve the contents of a double hash
[Ruby] How to batch convert strings in an array to numbers
ABC177 --solving E in Ruby
Validate JWT token in Ruby
Implemented XPath 1.0 parser in Ruby
Read design patterns in Ruby
Write class inheritance in Ruby
Update Ruby in Unicorn environment
Integer unified into Integer in Ruby 2.4
[Ruby] Exception handling in functions
Use ruby variables in javascript.
About regular expressions in Ruby
Birthday attack calculation in Ruby
Judgment of fractions in Ruby
[Ruby] Get array elements alternately.
Find Roman numerals in Ruby
Try using gRPC in Ruby
[Ruby] Find numbers in arrays
NCk mod p in Ruby
Chinese Remainder Theorem in Ruby
[Ruby] How to count even or odd numbers in an array
[Ruby] I want to put an array in a variable. I want to convert to an array
AtCoder ARC 081 C hash to solve in Ruby, Perl and Java