Ruby double hash

Inside the array is user_data, which has the information of multiple users as a hash.

user_data = [ {user: {profile: {name: 'Ken' }}}, 
              {user: {profile: {name: 'Ichiro' }}},
              {user: {profile: {name: 'Yuki' 
 }}},
]

#Output result in terminal
Ken
Ichiro
Yuki

What if you want to use user_data and code in Ruby so that only the names of all users are output?

To get a specific value from a hash

hash[The key of the value you want to get]

You can get it at. If you want to get a specific value from a double hash, you can get it by continuously specifying the key of the value you want to get.

hash[The key of the value you want to get][The key of the value you want to get]

In this case, I want to get the name, so

hash[:user][:profile][:name]

You can get it at.

Also, since the hashes are stored in the array, it is necessary to extract each hash and extract the name in each statement.

Therefore

user_data = [ {user: {profile: {name: 'Ken' }}}, 
              {user: {profile: {name: 'Ichiro' }}},
              {user: {profile: {name: 'Yuki' 
 }}},
]

#The name in

user_data.each do |u|
  puts u[:user][:profile][:name]
end

You can retrieve the name by writing as follows.

Recommended Posts

[ruby] Double hash
Ruby double hash
Extraction of "ruby" double hash * Review
Ruby Hash notes
[Ruby] Hash retrieval
[About double hash]
[Ruby] Extracting double hash in array
hash
Hash
[Ruby] How to retrieve the contents of a double hash
About Ruby single quotes and double quotes
Ruby learning 4
Ruby basics
Ruby learning 5
Ruby basics
Ruby Review 2
Ruby addition
Refactoring Ruby
Ruby learning 3
Ruby setting 2
Ruby problem ⑦
[Ruby] Block
Refactoring Ruby
ruby calculator
Ruby learning 6
Ruby settings 1
Refactoring Ruby
Ruby basics
Ruby memo
Ruby learning 1
Ruby Review 1
[Ruby] Module
About Ruby single quotes and double quotes
AtCoder ABC127 D hash to solve with Ruby 2.7.1