Project

General

Profile

Actions

Bug #16248

closed

Unclear semantics of the "numbered parameter is already used" error

Added by ibylich (Ilya Bylich) over 4 years ago. Updated over 4 years ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 2.7.0dev (2019-10-02T14:56:28Z trunk ef697388be) [x86_64-darwin18]
[ruby-core:95294]

Description

Numbered parameters now have an underscore as a prefix:

->{ _1 }.call(1)
 => 1

And it's not allowed to use them in a block if an outer block also uses them:

->{ _1; ->{_2} }
SyntaxError ((irb):4: numbered parameter is already used in)
(irb):4: outer block here
->{ _1; ->{_2} }
    ^~

even if the usage goes after declaring an inner block with numbered params:

->{ ->{_2}; _1 }
SyntaxError ((irb):5: numbered parameter is already used in)
(irb):5: inner block here
->{ ->{_2}; _1 }
       ^~

So far it's clear. Then comes the second part: if you wrap an inner block into a method you get a valid syntax:

->{ def m; ->{_2}; end; _1 }
 => #<Proc:0x00007f8666a25168 (irb):7 (lambda)>

but if you swap block statements you get an error:

>{ _1; def m; ->{_2}; end }
SyntaxError ((irb):6: numbered parameter is already used in)
(irb):6: outer block here
->{ _1; def m; ->{_2}; end }
    ^~

It looks like a consistency issue to me. Is there a strict rule that defines what is valid what is not?

Actions

Also available in: Atom PDF

Like0
Like0Like0