ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:110154] [Ruby master Bug#19033] One-liner pattern match as Boolean arg syntax error
@ 2022-09-30 22:03 baweaver (Brandon Weaver)
  2022-09-30 22:08 ` [ruby-core:110155] " baweaver (Brandon Weaver)
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: baweaver (Brandon Weaver) @ 2022-09-30 22:03 UTC (permalink / raw)
  To: ruby-core

Issue #19033 has been reported by baweaver (Brandon Weaver).

----------------------------------------
Bug #19033: One-liner pattern match as Boolean arg syntax error
https://bugs.ruby-lang.org/issues/19033

* Author: baweaver (Brandon Weaver)
* Status: Open
* Priority: Normal
* ruby -v: 3.0.2
* Backport: 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN
----------------------------------------
I was chatting earlier with Seb Wilgosz about pattern matching in tests, and suggested that he might consider the following:

```ruby
expect result in pattern
```

...but he reported back this will syntax error, including with parens:

```ruby
res = [:not_found, 999]
expect(res in [:not_found, *payload])
# => SyntaxError:
# /spec/app/interactors/articles/publish_spec.rb:13: syntax error, unexpected `in', expecting ')'
```

Interestingly though the following work:

```ruby
res = [:not_found, 999]

expect(res) in([:not_found, *payload])
# 1 example, 0 failures
expect(res) in([:not_found, 1, 3])
# 1 example, 0 failures
```

While this appears like an RSpec issue I would contend that it is reproducible with any other method that takes a boolean-like argument.

For me this feels like a syntax bug, but could see a case where it may be interpreted as ambiguous depending on the precedence of `in` relative to method arguments much like `method_name value if condition` is vague between `method_name(value) if condition` and `method_name(value if condition)`. That'll be especially difficult if it's `method_name value in pattern if condition`, so I do not envy parser writers here.

Would be curious for thoughts on that, or if we're looking at that wrong.



-- 
https://bugs.ruby-lang.org/

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

end of thread, other threads:[~2022-10-20  5:30 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-30 22:03 [ruby-core:110154] [Ruby master Bug#19033] One-liner pattern match as Boolean arg syntax error baweaver (Brandon Weaver)
2022-09-30 22:08 ` [ruby-core:110155] " baweaver (Brandon Weaver)
2022-09-30 22:45 ` [ruby-core:110156] " jeremyevans0 (Jeremy Evans)
2022-10-03 13:58 ` [ruby-core:110171] " Dan0042 (Daniel DeLorme)
2022-10-20  4:16 ` [ruby-core:110427] " matz (Yukihiro Matsumoto)
2022-10-20  5:28 ` [ruby-core:110429] " mame (Yusuke Endoh)
2022-10-20  5:30 ` [ruby-core:110430] " baweaver (Brandon Weaver)

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 read-only IMAP folder(s) and NNTP newsgroup(s).