Bug #16496
closedNumbered Parameter not parsed properly in lambda
Description
The space between *
and 1
changes the lambda behaviour, looks like numbered parameter _1
is not parsed properly
2.7.0 :001 > l = -> { _1 * 1 }
2.7.0 :002 > l[2]
=> 2
2.7.0 :003 > l2 = -> { _1 *1 }
2.7.0 :004 > l2[2]
Traceback (most recent call last):
5: from /Users/justin.feng/.rvm/rubies/ruby-2.7.0/bin/irb:23:in `<main>'
4: from /Users/justin.feng/.rvm/rubies/ruby-2.7.0/bin/irb:23:in `load'
3: from /Users/justin.feng/.rvm/rubies/ruby-2.7.0/lib/ruby/gems/2.7.0/gems/irb-1.2.1/exe/irb:11:in `<top (required)>'
2: from (irb):4
1: from (irb):3:in `block in irb_binding'
ArgumentError (wrong number of arguments (given 1, expected 0))
2.7.0 :005 >
Updated by mame (Yusuke Endoh) over 5 years ago
- Status changed from Open to Rejected
_1 *1
is parsed as _1(*1)
, a call to a method _1
with a variable-length argument with 1
. It is the same as p * ary
and p *ary
. You can see a warning under verbose mode:
$ ruby -w -e '-> { _1 *1 }.call'
-e:1: warning: `*' interpreted as argument prefix
...
It is difficult to change until a method name _1
is valid.
Updated by JustinFeng (Justin Feng) over 5 years ago
mame (Yusuke Endoh) wrote:
_1 *1
is parsed as_1(*1)
, a call to a method_1
with a variable-length argument with1
. It is the same asp * ary
andp *ary
. You can see a warning under verbose mode:$ ruby -w -e '-> { _1 *1 }.call' -e:1: warning: `*' interpreted as argument prefix ...
It is difficult to change until a method name
_1
is valid.
Hi Yusuke, thank you for the reply, good to understand what caused the issue
However, it just makes me feel the numbered parameter behaves different from traditional block parameter
2.7.0 :001 > l = lambda {|p| p *1 }
2.7.0 :002 > l[2]
=> 2
2.7.0 :003 > l = lambda {|p| p * 1 }
2.7.0 :004 > l[2]
=> 2
Updated by JustinFeng (Justin Feng) over 5 years ago
- Status changed from Rejected to Feedback
Updated by mame (Yusuke Endoh) over 5 years ago
We are now migrating. If we change it immediately, a program that uses _1
as a method name will break. So it is now warned:
warning: `_1' is reserved for numbered parameter; consider another name
After enough time, we will be able to prohibit _1
as a method name, and _1
can be always used as a numbered parameter. So please wait for a few years.
Updated by jeremyevans0 (Jeremy Evans) over 5 years ago
- Status changed from Feedback to Rejected