ruby-dev (Japanese) list archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-dev:52159] [Ruby Feature#21393] One-line pattern matching syntax as a method argument
@ 2025-06-02  3:00 koic (Koichi ITO) via ruby-dev
  2025-06-02  3:26 ` [ruby-dev:52160] " mame (Yusuke Endoh) via ruby-dev
  2025-06-02  3:29 ` [ruby-dev:52161] " koic (Koichi ITO) via ruby-dev
  0 siblings, 2 replies; 3+ messages in thread
From: koic (Koichi ITO) via ruby-dev @ 2025-06-02  3:00 UTC (permalink / raw)
  To: ruby-dev; +Cc: koic (Koichi ITO)

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/
_______________________________________________
ruby-dev mailing list -- ruby-dev@ml.ruby-lang.org
To unsubscribe send an email to ruby-dev-leave@ml.ruby-lang.org

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [ruby-dev:52160] [Ruby Feature#21393] One-line pattern matching syntax as a method argument
  2025-06-02  3:00 [ruby-dev:52159] [Ruby Feature#21393] One-line pattern matching syntax as a method argument koic (Koichi ITO) via ruby-dev
@ 2025-06-02  3:26 ` mame (Yusuke Endoh) via ruby-dev
  2025-06-02  3:29 ` [ruby-dev:52161] " koic (Koichi ITO) via ruby-dev
  1 sibling, 0 replies; 3+ messages in thread
From: mame (Yusuke Endoh) via ruby-dev @ 2025-06-02  3:26 UTC (permalink / raw)
  To: ruby-dev; +Cc: mame (Yusuke Endoh)

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/
_______________________________________________
ruby-dev mailing list -- ruby-dev@ml.ruby-lang.org
To unsubscribe send an email to ruby-dev-leave@ml.ruby-lang.org

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [ruby-dev:52161] [Ruby Feature#21393] One-line pattern matching syntax as a method argument
  2025-06-02  3:00 [ruby-dev:52159] [Ruby Feature#21393] One-line pattern matching syntax as a method argument koic (Koichi ITO) via ruby-dev
  2025-06-02  3:26 ` [ruby-dev:52160] " mame (Yusuke Endoh) via ruby-dev
@ 2025-06-02  3:29 ` koic (Koichi ITO) via ruby-dev
  1 sibling, 0 replies; 3+ messages in thread
From: koic (Koichi ITO) via ruby-dev @ 2025-06-02  3:29 UTC (permalink / raw)
  To: ruby-dev; +Cc: koic (Koichi ITO)

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/
_______________________________________________
ruby-dev mailing list -- ruby-dev@ml.ruby-lang.org
To unsubscribe send an email to ruby-dev-leave@ml.ruby-lang.org

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-06-02  3:29 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-06-02  3:00 [ruby-dev:52159] [Ruby Feature#21393] One-line pattern matching syntax as a method argument koic (Koichi ITO) via ruby-dev
2025-06-02  3:26 ` [ruby-dev:52160] " mame (Yusuke Endoh) via ruby-dev
2025-06-02  3:29 ` [ruby-dev:52161] " koic (Koichi ITO) via ruby-dev

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).