Extraction of "ruby" double hash * Review

ruby.rb



user_data = [
  {
   
 user: {
        profile: {
            name: 'takeshi'
        }
    }
  },
  {
    user: {
        profile: {
          name: 'tanaka'
        }
    }
  },
  {
    user: {
        profile: {
            name: 'tosiki'
        }
    }
  }
]


* Contents *

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! !! !! ** __

Implementation

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
!important

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.

result

__ I was able to output just the name brilliantly! !! __

  1. If you do not use each method, specify the subscripts one by one and describe the keys consecutively.
  2. I was able to concisely specify keys consecutively using each method
???????????@MacBook-Pro ruby % ruby posi.rb
takeshi
tanaka
tosiki

** Consideration **

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

Extraction of "ruby" double hash * Review
[ruby] Double hash
Ruby double hash
Review of Ruby basic grammar
About the behavior of ruby Hash # ==
Ruby Review 2
[Ruby] Extracting double hash in array
[Ruby] How to retrieve the contents of a double hash
Basics of Ruby ~ Review of confusing parts ~
Ruby / Rust linkage (6) Extraction of morphemes
[Ruby] Review about nesting of each
Ruby Basics 2 ~ Review of confusing parts ~
Ruby Review 1
Ruby Hash notes
[Ruby] Hash retrieval
[About double hash]
Basics of Ruby
NIO.2 review of java
Review of java Shilber
Utilization of Active Hash
definition of ruby method
Pitfalls of Active Hash
Ruby cherry book review
NIO review of java
Review the basic knowledge of ruby that is often forgotten
[Ruby] I want to reverse the order of the hash table
Basic methods of Ruby hashes
Basic methods of Ruby arrays
[Ruby] Various types of each
[Ruby] List of basic commands
Judgment of fractions in Ruby