Bug #18267
closedArgument forwarding requires parenthesis on method definitions
Description
I like to write my method definitions without parenthesis. For example:
def foo bar
# ...
end
I tried to use ...
, but it doesn't work. For example if I do:
def call a, ...
a.nil?(...)
end
It will be a syntax error, and the program exits. Can we make this syntax work so that I can always omit parenthesis?
Thank you!
Updated by matz (Yukihiro Matsumoto) about 3 years ago
I agree that this is a bug.
Matz.
Updated by byroot (Jean Boussier) about 3 years ago
It was known when the feature was introduced https://bugs.ruby-lang.org/projects/ruby-master/repository/git/revisions/d1ae2bc27fd4183e6abb9e83691e192bfe1e5316/diff, @mame (Yusuke Endoh) added a NEWS entry specifically for it.
I presume it would clash with endless ranges?
Updated by Eregon (Benoit Daloze) about 3 years ago
[joke] Maybe a reason to be a little bit more consistent about Ruby syntax and always use parens for method definitions? :D [/joke]
Updated by Eregon (Benoit Daloze) about 3 years ago
On a more serious note, def=
also has this restriction:
$ ruby -e 'def m(a) = 2'
OK
$ ruby -e 'def m a = 2'
-e:1: syntax error, unexpected end-of-input
$ ruby -e 'def m a = a'
-e:1: circular argument reference - a
-e:1: syntax error, unexpected end-of-input
Which I guess is trivially ambiguous because the =
could be for an optional arg or for the method body.
Because (...)
anyway needs parenthesis on call sites (ambiguous with endless ranges otherwise), I personally think it makes sense to also have parens as a parameter.
Updated by nobu (Nobuyoshi Nakada) about 3 years ago
- Status changed from Open to Closed
Applied in changeset git|13a9597c7ca83fced5738e9345660ae6aef87eb7.
Argument forwarding definition without parentheses [Bug #18267]