Actions
Bug #16248
closedUnclear semantics of the "numbered parameter is already used" error
Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 2.7.0dev (2019-10-02T14:56:28Z trunk ef697388be) [x86_64-darwin18]
Backport:
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?
Updated by nobu (Nobuyoshi Nakada) almost 5 years ago
- Status changed from Open to Closed
Applied in changeset git|c2065c64cb198d5b53a5ecdf9638d064f6fec2e2.
Fixed numbered parameter check
- parse.y (struct local_vars): moved numbered parameter NODEs for
nesting check to separate per local variable scopes, as numbered
parameters should belong to local variable scopes. [Bug #16248]
Updated by nobu (Nobuyoshi Nakada) almost 5 years ago
- Backport changed from 2.5: UNKNOWN, 2.6: UNKNOWN to 2.5: DONTNEED, 2.6: DONTNEED
Actions
Like0
Like0Like0