Project

General

Profile

Actions

Feature #11605

closed

Ruby should raise on def(a='a', b) [optional param that can't be used]

Added by rosenfeld (Rodrigo Rosenfeld Rosas) over 8 years ago. Updated over 2 years ago.

Status:
Rejected
Target version:
-
[ruby-core:71115]

Description

Why doesn't Ruby complain when defining a method like this?

def a(opt='opt', required)
end

I believe it's impossible to call method 'a' passing only required, while sticking to the default value for 'opt', right? So, why should Ruby allow such definition?

Updated by usa (Usaku NAKAMURA) over 8 years ago

  • Status changed from Open to Rejected

Why did not you try actually?

irb(main):001:0> def a(opt='opt',required)
irb(main):002:1>   p [opt, required]
irb(main):003:1> end
=> :a
irb(main):004:0> a('required')
["opt", "required"]
=> ["opt", "required"]

Updated by rosenfeld (Rodrigo Rosenfeld Rosas) over 8 years ago

Oh, thanks. I had no idea on how to try this. I've tried a(, 'required'). Actually, I was reading a method like a(req1, opt='opt', req2) and thought it would be impossible to use the default value, but now I understand how optional arguments work in Ruby. Sorry for the confusion.

Actions #3

Updated by hsbt (Hiroshi SHIBATA) over 2 years ago

  • Project changed from 14 to Ruby master
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0