2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Rubyハッシュからの取り出し

Last updated at Posted at 2019-08-11

ハッシュからの取り出しが頭から抜けていたので復習もかねて。
ターミナルから以下のデータを取り出す。

George
Alice
Taro

問題


user_data = [
  {
    user: {
        profile: {
            name: 'George'
        }
    }
  },
  {
    user: {
        profile: {
          name: 'Alice'
        }
    }
  },
  {
    user: {
        profile: {
            name: 'Taro'
        }
    }
  }
]

答え

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

あるいは

user_data.each{ |u| puts u.dig(:user, :profile, :name) }
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?