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=-3.1 required=3.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, FORGED_GMAIL_RCVD,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, 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 3DE6F1F462 for ; Wed, 22 May 2019 09:52:15 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 5D325120946; Wed, 22 May 2019 18:52:09 +0900 (JST) Received: from o1678948x4.outbound-mail.sendgrid.net (o1678948x4.outbound-mail.sendgrid.net [167.89.48.4]) by neon.ruby-lang.org (Postfix) with ESMTPS id C0A66120A5D for ; Wed, 22 May 2019 18:52:06 +0900 (JST) Received: by filter0098p3iad2.sendgrid.net with SMTP id filter0098p3iad2-16369-5CE51BC7-1B 2019-05-22 09:52:07.505389049 +0000 UTC m=+558793.374477986 Received: from herokuapp.com (unknown [3.81.19.97]) by ismtpd0065p1mdw1.sendgrid.net (SG) with ESMTP id 6AiRmM26SDaSkTqGo2MzRQ for ; Wed, 22 May 2019 09:52:07.377 +0000 (UTC) Date: Wed, 22 May 2019 09:52:07 +0000 (UTC) From: bogdanvlviv@gmail.com Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 68266 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 15831 X-Redmine-Issue-Author: bogdanvlviv X-Redmine-Sender: bogdanvlviv 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?JQn=2F3KuDOUId4eQ7B+5pSv9j73ZklmG1Z68xEfikn7I6Dj+LGs8dZSS3OTJOOz?= =?us-ascii?Q?ccVx53dRcB1Hg+FwtrzYS+0AMpjL3J+4eRAUXhB?= =?us-ascii?Q?c6q2s05S5INyBj0Z3PhI0BiQ3yQ4bipJuahBBHU?= =?us-ascii?Q?LBVfq1d=2FiuK45dCLG094M9YrsrE488WKF2gB51d?= =?us-ascii?Q?Dk2yhLH1O4gtBybSb=2F0IqAJuYa48VfFwsKQ=3D=3D?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 92780 Subject: [ruby-core:92780] [Ruby trunk Feature#15831] Add `Array#extract`, `Hash#extract`, and `ENV.extract` 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 #15831 has been updated by bogdanvlviv (Bogdan Denkovych). matz (Yukihiro Matsumoto) wrote: > I don't think we have seen the use-case that this method is absolutely necessary. I also concern about the name conflict with the ActiveSupport method. > > Let me see the real-world use-case, please. > > Matz. There are use-cases of `Array#extract` in this Pull Request https://github.com/rails/rails/pull/33137/files ---------------------------------------- Feature #15831: Add `Array#extract`, `Hash#extract`, and `ENV.extract` https://bugs.ruby-lang.org/issues/15831#change-78145 * Author: bogdanvlviv (Bogdan Denkovych) * Status: Rejected * Priority: Normal * Assignee: * Target version: ---------------------------------------- ## Add `Array#extract` The method removes and returns the elements for which the block returns a true value. If no block is given, an Enumerator is returned instead. ```ruby numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] odd_numbers = numbers.extract { |number| number.odd? } # => [1, 3, 5, 7, 9] numbers # => [0, 2, 4, 6, 8] ``` This method was added to Active Support as `extract!` in https://github.com/rails/rails/pull/33137 In this post, you can find use cases of this method https://bogdanvlviv.com/posts/ruby/rails/array-extract-to-activesupport-6-0.html ## Add `Hash#extract` The method removes and returns the key/value pairs for which the block evaluates to +true+. If no block is given, an Enumerator is returned instead. ```ruby h = {a: 100, b: 200, c: 300} h.extract {|k, v| v > 150} # => {:b=>200, :c=>300} h # => {:a=>100} ``` Note that there is method `extract!` in Active Support that was added in 2009, see https://github.com/rails/rails/commit/8dcf91ca113579646e95b0fd7a864dfb6512a53b But I think we should upstream `extract!` to Ruby as `slice!`. ## Add `ENV.extract` The method removes and returns the key/value pairs for which the block evaluates to +true+. If no block is given, an Enumerator is returned instead. ```ruby ENV.extract {|k, v| k == "PORT"} # => {"PORT"=>"3000"} ``` Pull Request: https://github.com/ruby/ruby/pull/2171 Patch: https://patch-diff.githubusercontent.com/raw/ruby/ruby/pull/2171.patch -- https://bugs.ruby-lang.org/