From: cottrey@zendesk.com
To: ruby-core@ruby-lang.org
Subject: [ruby-core:99228] [Ruby master Feature#17036] Regexp deconstruction keys to allow pattern matching
Date: Sun, 19 Jul 2020 14:21:59 +0000 (UTC) [thread overview]
Message-ID: <redmine.issue-17036.20200719142158.41933@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-17036.20200719142158.41933@ruby-lang.org
Issue #17036 has been reported by zechris (Chris Ottrey).
----------------------------------------
Feature #17036: Regexp deconstruction keys to allow pattern matching
https://bugs.ruby-lang.org/issues/17036
* Author: zechris (Chris Ottrey)
* Status: Open
* Priority: Normal
----------------------------------------
This is to allow Regexp matches in Ruby 2.7's new experimental Pattern Matching.
eg.
```ruby
case /(?<a>.)(?<b>.)/.match("xy")
in a: "x", b:
"a was 'x' and b was matched to #{b.inspect}"
end
#=> "a was 'x' and b was matched to 'y'"
```
`MatchData#named_captures` is *close* but not close enough as the Hash required from a `deconstructed_keys()` method that gets used during Pattern Matching requires symbolized keys.
### **NB. This PR ( https://github.com/ruby/ruby/pull/3333) is WIP as the C code hasn't actually been written yet...**
Although, it could be used in the wild now with a refinement of `MatchData` like so:
```ruby
module RegexpDeconstructKeys
refine MatchData do
# Temporary patch in ruby to make tests pass until the code gets re-written in C
def deconstruct_keys(*keys)
h0 = named_captures.transform_keys(&:to_sym)
if keys
keys = keys.flatten.compact
raise TypeError unless keys.all? { |k| k.is_a?(Symbol) }
h0 = h0.slice(*keys) unless keys.empty?
end
h0.inject({}) { |h, (k, v)| h[k] = v if v; h }
end
# Temporary patch in ruby to make tests pass until the code gets re-written in C
end
end
using RegexpDeconstructKeys
case /(?<a>.)(?<b>.)/.match("xy")
in a: "x", b:
puts "a was 'x' and b was #{b.inspect}"
end
#=> "a was 'x' and b was matched to 'y'"
```
(tested in `ruby v2.7.1`)
--
https://bugs.ruby-lang.org/
parent reply other threads:[~2020-07-19 14:22 UTC|newest]
Thread overview: expand[flat|nested] mbox.gz Atom feed
[parent not found: <redmine.issue-17036.20200719142158.41933@ruby-lang.org>]
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.issue-17036.20200719142158.41933@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).