Extraction of "ruby" double hash * Review


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



#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"]


#each method usage pattern

user_date.each do |date|
  puts date [:user][:profile][:name]

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

  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
??[email protected] ruby % ruby posi.rb

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