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 3 years have passed since last update.

(ボツ) #Ruby でぼっち演算子 &. を使って、ネストしたハッシュから #Rails の dig みたいに値を取り出し、 nil エラーの例外も起こさない。

0
Last updated at Posted at 2019-10-25

hash[:key] と書くところを

hash&.[](:key) と書けば良いっぽい。

{a: { b: :c } }&.[](:a)&.[](:b)
=> :c

{a: nil }&.[](:a)&.[](:b)
=> nil

nil&.[](:a)&.[](:b)
=> nil

わりと分かりづらい。

Original by Github issue

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

Twitter

0
0
2

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?