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-ASN: AS4713 221.184.0.0/13 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 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 B85E11F462 for ; Wed, 22 May 2019 07:44:41 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 6172B120A2B; Wed, 22 May 2019 16:44:36 +0900 (JST) Received: from o1678916x28.outbound-mail.sendgrid.net (o1678916x28.outbound-mail.sendgrid.net [167.89.16.28]) by neon.ruby-lang.org (Postfix) with ESMTPS id D67AC120A19 for ; Wed, 22 May 2019 16:44:33 +0900 (JST) Received: by filter0058p3mdw1.sendgrid.net with SMTP id filter0058p3mdw1-17027-5CE4FDE2-21 2019-05-22 07:44:35.020592605 +0000 UTC m=+21710.773533158 Received: from herokuapp.com (unknown [3.81.19.97]) by ismtpd0005p1iad2.sendgrid.net (SG) with ESMTP id 0U74ZB0xT5GleyBbP8RX4g for ; Wed, 22 May 2019 07:44:34.707 +0000 (UTC) Date: Wed, 22 May 2019 07:44:35 +0000 (UTC) From: matz@ruby.or.jp Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 68257 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 15863 X-Redmine-Issue-Author: bogdanvlviv X-Redmine-Sender: matz 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?bXEIHGfdFwsIlBTndiToCp=2Fmc2rfxRD2sZAksRKJIHXvMB8MBX04=2FKqufdz580?= =?us-ascii?Q?9q2PjeNvacYpEIKIYspD8aPs5SirRhlMs360C23?= =?us-ascii?Q?PxRFAipmjnYTnuFYruVge3PaTkhmQuCEE5k4cVr?= =?us-ascii?Q?fPVv4J8itbkaq90Zzuu6JCLXWEbh4p33U66otQP?= =?us-ascii?Q?Fl0EOt+henm2cFum22mCyOSjBppRSrs=2F7WQ=3D=3D?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 92771 Subject: [ruby-core:92771] [Ruby trunk Feature#15863] Add `Hash#slice!` and `ENV.slice!` 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 #15863 has been updated by matz (Yukihiro Matsumoto). Status changed from Open to Rejected I don't think we have seen the use-case that this method is absolutely necessary. Let me see the real-world use-case, please. Matz. ---------------------------------------- Feature #15863: Add `Hash#slice!` and `ENV.slice!` https://bugs.ruby-lang.org/issues/15863#change-78136 * Author: bogdanvlviv (Bogdan Denkovych) * Status: Rejected * Priority: Normal * Assignee: * Target version: ---------------------------------------- ## Add `Hash#slice!` In https://bugs.ruby-lang.org/issues/8499 we added `Hash#slice`. `Hash#slice!` removes and returns the key/value pairs matching the given keys: ```ruby h = {a: 100, b: 200, c: 300} h.slice!(:a) # => {:a=>100} h # => {:b=>200, :c=>300} h.slice!(:b, :c, :d) # => {:b=>200, :c=>300} h # => {} ``` Note that, this method reflects the behavior of Active Support's `Hash#extract!` method that was added in 2009, see https://github.com/rails/rails/commit/8dcf91ca113579646e95b0fd7a864dfb6512a53b ```ruby h = {a: 100, b: 200, c: 300} h.extract!(:a) # => {:a=>100} h # => {:b=>200, :c=>300} h.extract!(:b, :c, :d) # => {:b=>200, :c=>300} h # => {} ``` (There is a proposal to add `Hash#extract` to Ruby - https://bugs.ruby-lang.org/issues/15831, but it has another method signature) Active Support also has `Hash#slice!`, see https://api.rubyonrails.org/v5.2/classes/Hash.html#method-i-slice-21. It is quite different what this patch proposes, see how it works: ```ruby h = {a: 100, b: 200, c: 300} h.slice!(:a) # => {:b=>200, :c=>300} # AS Hash#slice! h # => {:a=>100} h.slice!(:b, :c, :d) # => {:a=>100} # AS Hash#slice! h # => {} ``` I think `Hash#slice!` in Ruby should work in the same way as `Hash#extract!` from Active Support, there is one argument: - https://bugs.ruby-lang.org/issues/8499#note-31 It should behave in the way `Hash#slice` does, except one thing `Hash#slice!` modifies the object. (See, for instance, how `Array#slice` and `Array#slice!` work, they return the same value) But I would like to discuss it more to choose the right behavior for the proposed method. (Maybe there are good arguments why we should add `Hash#slice!` with behavior as it is in Active Support) ## Add `ENV.slice!` The method removes and returns the key/value pairs matching the given keys. ```ruby ENV.slice!("PORT", "RAILS_ENV") # => {"PORT"=>"3000", "RAILS_ENV"=>"development"} ``` Pull Request: https://github.com/ruby/ruby/pull/2195 Patch: https://patch-diff.githubusercontent.com/raw/ruby/ruby/pull/2195.patch -- https://bugs.ruby-lang.org/