LoginSignup
0
0

More than 3 years have passed since last update.

Rubyでメソッドの引数にnilを渡すと、デフォルト引数は適用されない。

Posted at

タイトルのとおりで初歩的な話ながら、知らなかったのでメモ。

def add_one(num = 10)
  num + 1
end

p add_one(3)  # => 4

p add_one  # => 11

p add_one(nil)  # => undefined method `+' for nil:NilClass (NoMethodError)
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