Project

General

Profile

Actions

Feature #21393

closed

One-line pattern matching syntax as a method argument

Added by koic (Koichi ITO) 5 days ago. Updated 4 days ago.

Status:
Feedback
Assignee:
-
Target version:
-
[ruby-dev:<unknown>]

Description

Context

Using one-line in pattern matching syntax as a method argument causes a syntax error.

$ 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:

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

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

$ 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

Actions

Also available in: Atom PDF

Like0
Like1Like0Like0