From: janosch-x via ruby-core Date: 2026-11-23T18:53:31+00:00 Subject: [ruby-core:127686] [Ruby Feature#17036] Regexp deconstruction keys to allow pattern matching Issue #17036 has been updated by janosch-x (Janosch M�ller). This has long since been implemented, in issue #18821 via [PR 6216](https://github.com/ruby/ruby/pull/6216). ---------------------------------------- Feature #17036: Regexp deconstruction keys to allow pattern matching https://bugs.ruby-lang.org/issues/17036#change-120198 * Author: zechris (Chris Ottrey) * Status: Open ---------------------------------------- This is to allow Regexp matches in Ruby 2.7's new experimental Pattern Matching. eg. ```ruby case /(?.)(?.)/.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`](https://ruby-doc.org/core-2.7.0/MatchData.html#method-i-named_captures) is *close* but not close enough as the Hash required from a [`deconstruct_keys()`](https://github.com/ruby/ruby/blob/cab9788e0ca2c41a13e482a4a9924b5a968284fe/doc/syntax/pattern_matching.rdoc#matching-non-primitive-objects-deconstruct_keys-and-deconstruct-) 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 /(?.)(?.)/.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/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/