Project

General

Profile

Actions

Feature #21393

closed

One-line pattern matching syntax as a method argument

Added by koic (Koichi ITO) 3 days ago. Updated 2 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 #1

Updated by mame (Yusuke Endoh) 3 days ago

I understand your expectation, but if we allow this, the syntax would be ambiguous. I am not sure which

do_something(expr in a, b, c)

should be interpreted as

do_something((expr in a), b, c)

or

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.

Actions #2

Updated by koic (Koichi ITO) 3 days ago

That code is not a pattern matching. It is interpreted as a keyword argument.

Oops! This was entirely my misunderstanding.

Actions #3

Updated by nobu (Nobuyoshi Nakada) 2 days ago

  • Status changed from Open to Feedback
Actions

Also available in: Atom PDF

Like0
Like1Like0Like0