[ruby-dev:52159] [Ruby Feature#21393] One-line pattern matching syntax as a method argument

Issue #21393 has been reported by koic (Koichi ITO). ---------------------------------------- Feature #21393: One-line pattern matching syntax as a method argument https://bugs.ruby-lang.org/issues/21393 * Author: koic (Koichi ITO) * Status: Open ---------------------------------------- ## Context Using one-line `in` pattern matching syntax as a method argument causes a syntax error. ```console $ ruby -vce 'do_something(expression in pattern)' ruby 3.4.4 (2025-05-14 revision a38531fd3f) +PRISM [x86_64-darwin24] ruby: -e:1: syntax errors found (SyntaxError)
1 | do_something(expression in pattern) | ^~ unexpected 'in'; expected a `)` to close the arguments | ^ unexpected ')', ignoring it | ^ unexpected ')', expecting end-of-input
Wrapping the argument in parentheses avoids the syntax error:
```console
$ ruby -vce 'do_something((expression in pattern))'
ruby 3.4.4 (2025-05-14 revision a38531fd3f) +PRISM [x86_64-darwin24]
Syntax OK
This was unexpected, as I assumed `do_something(expression in pattern)` would work without parentheses. For reference, `do_something(expression => pattern)`, which results in a void context, is valid syntax. ```console $ ruby -vce 'do_something(expression => pattern)' ruby 3.4.4 (2025-05-14 revision a38531fd3f) +PRISM [x86_64-darwin24] Syntax OK ``` ## Proposal It seems more appropriate that `do_something(expression in pattern)` should also be valid syntax. ## Additional Information This behavior is consistent not only in Prism, but also in `parse.y`. ```console $ ruby --parser=parse.y -vce 'do_something(expression in pattern)' ruby 3.4.4 (2025-05-14 revision a38531fd3f) [x86_64-darwin24] -e:1: syntax error, unexpected 'in', expecting ')' do_something(expression in pattern) ruby: compile error (SyntaxError) ``` I encountered this while working on a RuboCop issue: https://github.com/rubocop/rubocop/issues/14217 -- https://bugs.ruby-lang.org/

Issue #21393 has been updated by mame (Yusuke Endoh). I understand your expectation, but if we allow this, the syntax would be ambiguous. I am not sure which ```ruby do_something(expr in a, b, c) ``` should be interpreted as ```ruby do_something((expr in a), b, c) ``` or ```ruby do_something((expr in [a, b, c])) ``` .
For reference, `do_something(expression => pattern)`, which results in a void context, is valid syntax.
That code is not a pattern matching. It is interpreted as a keyword argument. ---------------------------------------- Feature #21393: One-line pattern matching syntax as a method argument https://bugs.ruby-lang.org/issues/21393#change-113520 * Author: koic (Koichi ITO) * Status: Open ---------------------------------------- ## Context Using one-line `in` pattern matching syntax as a method argument causes a syntax error. ```console $ ruby -vce 'do_something(expression in pattern)' ruby 3.4.4 (2025-05-14 revision a38531fd3f) +PRISM [x86_64-darwin24] ruby: -e:1: syntax errors found (SyntaxError)
1 | do_something(expression in pattern) | ^~ unexpected 'in'; expected a `)` to close the arguments | ^ unexpected ')', ignoring it | ^ unexpected ')', expecting end-of-input
Wrapping the argument in parentheses avoids the syntax error:
```console
$ ruby -vce 'do_something((expression in pattern))'
ruby 3.4.4 (2025-05-14 revision a38531fd3f) +PRISM [x86_64-darwin24]
Syntax OK
This was unexpected, as I assumed `do_something(expression in pattern)` would work without parentheses. For reference, `do_something(expression => pattern)`, which results in a void context, is valid syntax. ```console $ ruby -vce 'do_something(expression => pattern)' ruby 3.4.4 (2025-05-14 revision a38531fd3f) +PRISM [x86_64-darwin24] Syntax OK ``` ## Proposal It seems more appropriate that `do_something(expression in pattern)` should also be valid syntax. ## Additional Information This behavior is consistent not only in Prism, but also in `parse.y`. ```console $ ruby --parser=parse.y -vce 'do_something(expression in pattern)' ruby 3.4.4 (2025-05-14 revision a38531fd3f) [x86_64-darwin24] -e:1: syntax error, unexpected 'in', expecting ')' do_something(expression in pattern) ruby: compile error (SyntaxError) ``` I encountered this while working on a RuboCop issue: https://github.com/rubocop/rubocop/issues/14217 -- https://bugs.ruby-lang.org/

Issue #21393 has been updated by koic (Koichi ITO).
That code is not a pattern matching. It is interpreted as a keyword argument.
Oops! This was entirely my misunderstanding. ---------------------------------------- Feature #21393: One-line pattern matching syntax as a method argument https://bugs.ruby-lang.org/issues/21393#change-113521 * Author: koic (Koichi ITO) * Status: Open ---------------------------------------- ## Context Using one-line `in` pattern matching syntax as a method argument causes a syntax error. ```console $ ruby -vce 'do_something(expression in pattern)' ruby 3.4.4 (2025-05-14 revision a38531fd3f) +PRISM [x86_64-darwin24] ruby: -e:1: syntax errors found (SyntaxError)
1 | do_something(expression in pattern) | ^~ unexpected 'in'; expected a `)` to close the arguments | ^ unexpected ')', ignoring it | ^ unexpected ')', expecting end-of-input
Wrapping the argument in parentheses avoids the syntax error:
```console
$ ruby -vce 'do_something((expression in pattern))'
ruby 3.4.4 (2025-05-14 revision a38531fd3f) +PRISM [x86_64-darwin24]
Syntax OK
This was unexpected, as I assumed `do_something(expression in pattern)` would work without parentheses. For reference, `do_something(expression => pattern)`, which results in a void context, is valid syntax. ```console $ ruby -vce 'do_something(expression => pattern)' ruby 3.4.4 (2025-05-14 revision a38531fd3f) +PRISM [x86_64-darwin24] Syntax OK ``` ## Proposal It seems more appropriate that `do_something(expression in pattern)` should also be valid syntax. ## Additional Information This behavior is consistent not only in Prism, but also in `parse.y`. ```console $ ruby --parser=parse.y -vce 'do_something(expression in pattern)' ruby 3.4.4 (2025-05-14 revision a38531fd3f) [x86_64-darwin24] -e:1: syntax error, unexpected 'in', expecting ')' do_something(expression in pattern) ruby: compile error (SyntaxError) ``` I encountered this while working on a RuboCop issue: https://github.com/rubocop/rubocop/issues/14217 -- https://bugs.ruby-lang.org/
participants (2)
-
koic (Koichi ITO)
-
mame (Yusuke Endoh)