From: "baweaver (Brandon Weaver)" <noreply@ruby-lang.org>
To: ruby-core@ruby-lang.org
Subject: [ruby-core:110155] [Ruby master Bug#19033] One-liner pattern match as Boolean arg syntax error
Date: Fri, 30 Sep 2022 22:08:59 +0000 (UTC) [thread overview]
Message-ID: <redmine.journal-99412.20220930220859.7576@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-19033.20220930220307.7576@ruby-lang.org
Issue #19033 has been updated by baweaver (Brandon Weaver).
Addendum: We did find that `expect((value in pattern))` works, but not `expect(value in pattern)`, which is where we find things to be confusing
----------------------------------------
Bug #19033: One-liner pattern match as Boolean arg syntax error
https://bugs.ruby-lang.org/issues/19033#change-99412
* 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/
next prev parent reply other threads:[~2022-09-30 22:09 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
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 ` baweaver (Brandon Weaver) [this message]
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)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-list from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.ruby-lang.org/en/community/mailing-lists/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=redmine.journal-99412.20220930220859.7576@ruby-lang.org \
--to=ruby-core@ruby-lang.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).