Project

General

Profile

Bug #16496

Numbered Parameter not parsed properly in lambda

Added by JustinFeng (Justin Feng) 9 months ago. Updated 8 months ago.

Status:
Rejected
Priority:
Normal
Assignee:
-
Target version:
-
ruby -v:
ruby 2.7.0p0 (2019-12-25 revision 647ee6f091) [x86_64-darwin18]
[ruby-core:96750]

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) 9 months 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) 9 months ago

mame (Yusuke Endoh) wrote:

_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.

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 
#3

Updated by JustinFeng (Justin Feng) 9 months ago

  • Status changed from Rejected to Feedback

Updated by mame (Yusuke Endoh) 9 months 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.

#5

Updated by jeremyevans0 (Jeremy Evans) 8 months ago

  • Status changed from Feedback to Rejected

Also available in: Atom PDF