From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-Status: No, score=-4.1 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,UNPARSEABLE_RELAY shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from neon.ruby-lang.org (neon.ruby-lang.org [221.186.184.75]) by dcvr.yhbt.net (Postfix) with ESMTP id E8EFE1F5AE for ; Sun, 19 Jul 2020 14:22:05 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 97CF71209C1; Sun, 19 Jul 2020 23:21:35 +0900 (JST) Received: from xtrwkhkc.outbound-mail.sendgrid.net (xtrwkhkc.outbound-mail.sendgrid.net [167.89.16.28]) by neon.ruby-lang.org (Postfix) with ESMTPS id C20BF1209B1 for ; Sun, 19 Jul 2020 23:21:32 +0900 (JST) Received: by filterdrecv-p3las1-7754f7d4cc-tg96p with SMTP id filterdrecv-p3las1-7754f7d4cc-tg96p-18-5F145706-5F 2020-07-19 14:21:59.023506463 +0000 UTC m=+2059709.731498619 Received: from herokuapp.com (unknown) by geopod-ismtpd-2-3 (SG) with ESMTP id ejmFjkLNROSEjmvC82I40A for ; Sun, 19 Jul 2020 14:21:58.816 +0000 (UTC) Date: Sun, 19 Jul 2020 14:21:59 +0000 (UTC) From: cottrey@zendesk.com Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 75017 X-Redmine-Project: ruby-master X-Redmine-Issue-Tracker: Feature X-Redmine-Issue-Id: 17036 X-Redmine-Issue-Author: zechris X-Redmine-Sender: zechris X-Mailer: Redmine X-Redmine-Host: bugs.ruby-lang.org X-Redmine-Site: Ruby Issue Tracking System X-Auto-Response-Suppress: All Auto-Submitted: auto-generated X-SG-EID: =?us-ascii?Q?uTR3Na1Nh3pAKW+2u0jTper4nRu9CcDk3ZjJWveRbf2UUXwxq8wgrU0ntpP4vO?= =?us-ascii?Q?5mJiKeR+IR1k=2FtQC+KqH8K+7Jpp=2F89MwKCDpCV6?= =?us-ascii?Q?OXutfUyW6W4Wdr1QOADYEzP3Z0wni4ijJ8hoA3R?= =?us-ascii?Q?3FEqN6eXIBOl3hFFjbS+3DaWV9X9BLrASZTwmaw?= =?us-ascii?Q?yuWtO3l20z+877idpQBhE8pI1cff2e1ugMB1gYs?= =?us-ascii?Q?PtSpnLIHPGYSAS=2FNU=3D?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 99228 Subject: [ruby-core:99228] [Ruby master Feature#17036] Regexp deconstruction keys to allow pattern matching X-BeenThere: ruby-core@ruby-lang.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: Ruby developers List-Id: Ruby developers List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ruby-core-bounces@ruby-lang.org Sender: "ruby-core" 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 /(?.)(?.)/.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 /(?.)(?.)/.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/