Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#Ruby 配列/ハッシュでぼっち演算子を利用して nil エラーを防ぐ ( NoMethodError: undefined method `[]' for nil:NilClass ) #3166

Open
YumaInaura opened this issue May 14, 2020 · 0 comments

Comments

@YumaInaura
Copy link
Owner

[] メソッド対して引数を与えれば良い

Array

[:a, :b, :c][0]
# => :a

nil[0]
# NoMethodError: undefined method `[]' for nil:NilClass

nil&.[](0)
# nil

Hash

{a: :x}[:a]
# => :x

nil[:a]
# NoMethodError: undefined method `[]' for nil:NilClass

nil&.[](:a)
# nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant