[Ruby] [Ruby] How to retrieve the contents of double hash

less than 1 minute read

Overview

I will summarize as a memorandum about how to retrieve the double hash.

environment

Ruby 2.6.5

Content

For example, suppose you have a variable user_date with multiple hashes inside an array.

user_data = [
   {
     user: {
       profile: {
         name:'Taro'
       }
     }
   },
   {
     user: {
       profile: {
         name:'Jiro'
       }
     }
   },
   {
     user: {
       profile: {
         name:'Saburo'
       }
     }
   }
]

To get the value of the hash, specify the key corresponding to that value.

Hash [key of value to retrieve]

If you want to get specific data from the double hash, specify the key of the value to be acquired in succession up to the data to be acquired. Therefore. If you try to output only the names of all users, it will be as follows.

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

that’s all

Tags:

Updated: