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,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,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 2E1011F461 for ; Tue, 14 May 2019 00:33:32 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id BFBD3120953; Tue, 14 May 2019 09:33:26 +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 F39D5120927 for ; Tue, 14 May 2019 09:33:24 +0900 (JST) Received: by filter0041p3las1.sendgrid.net with SMTP id filter0041p3las1-25373-5CDA0CD5-1C 2019-05-14 00:33:25.51459513 +0000 UTC m=+23538.208060227 Received: from herokuapp.com (unknown [18.212.173.8]) by ismtpd0050p1iad1.sendgrid.net (SG) with ESMTP id EV6gXPUMQtWKZe-RXC65YQ for ; Tue, 14 May 2019 00:33:25.427 +0000 (UTC) Date: Tue, 14 May 2019 00:33:25 +0000 (UTC) From: nobu@ruby-lang.org Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 68121 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 15831 X-Redmine-Issue-Author: bogdanvlviv X-Redmine-Sender: nobu 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?q8Dly+pU2+3ektTtZVXgZtbJPXwqo7p86jCsvYTW4Byf9UWCFJ8+wfiP=2F2d6U2?= =?us-ascii?Q?ikhybJNnnoMogazMEfXAbLCtCx6xLS2pdvu1jtt?= =?us-ascii?Q?TLI=2FsvjbA0x8yRnyOo4jPaotN96Fr3fjp=2FRWjBY?= =?us-ascii?Q?vbe+L9IYC3lQ=2Fel1JJLUEKi0AnRyIQVEv+RNSi0?= =?us-ascii?Q?YaSf5=2FXKO49ZHY01tPQhGiR1tWB+Adno+rQ=3D=3D?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 92638 Subject: [ruby-core:92638] [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 nobu (Nobuyoshi Nakada). Why `Array#extract` has no argument but takes a block, while `Hash` and `ENV` are opposite? ---------------------------------------- Feature #15831: Add `Array#extract`, `Hash#extract`, and `ENV::extract` https://bugs.ruby-lang.org/issues/15831#change-78000 * Author: bogdanvlviv (Bogdan Denkovych) * Status: Open * 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 matching the given keys. ```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 # => {} ``` This method was added to Active Support as `extract!` in 2009, see https://github.com/rails/rails/commit/8dcf91ca113579646e95b0fd7a864dfb6512a53b ## Add `ENV::extract` The method removes and returns the key/value pairs matching the given keys. ```ruby ENV.extract("PORT", "RAILS_ENV") # => {"PORT"=>"3000", "RAILS_ENV"=>"development"} ``` Pull Request: https://github.com/ruby/ruby/pull/2171 -- https://bugs.ruby-lang.org/