0
0

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 1 year has passed since last update.

二重ハッシュについて

Posted at

目的

二重ハッシュから任意の値を取り出す方法を記録する。

二重ハッシュから任意の値を取り出す方法

取得したい値のキーまで連続して指定すると取得できます。

ハッシュ[:取得したい値のキー名][:取得したい値のキー名]

<例>
user_data = [
 {user: {profile: {name: 'George'}}},
 {user: {profile: {name: 'Alice'}}},
 {user: {profile: {name: 'Taro'}}},
]
からnameキーの値を取り出すには、
user_data.each do |u|
  puts u[:user][:profile][:name]
end

もしくは、digメソッドを用いて
user_data.each{ |u| puts u.dig(:user, :profile, :name) }

補足

digメソッドについては、
https://techtechmedia.com/dig/
の記事がわかりやすい。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?