ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: matz@ruby.or.jp
To: ruby-core@ruby-lang.org
Subject: [ruby-core:92772] [Ruby trunk Feature#15831] Add `Array#extract`, `Hash#extract`, and `ENV.extract`
Date: Wed, 22 May 2019 07:45:39 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-78137.20190522074539.9758acdee4e10446@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-15831.20190505205201@ruby-lang.org

Issue #15831 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. I also concern about the name conflict with the ActiveSupport method.

Let me see the real-world use-case, please.

Matz.

----------------------------------------
Feature #15831: Add `Array#extract`, `Hash#extract`, and `ENV.extract`
https://bugs.ruby-lang.org/issues/15831#change-78137

* 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/

  parent reply	other threads:[~2019-05-22  7:45 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <redmine.issue-15831.20190505205201@ruby-lang.org>
2019-05-05 20:52 ` [ruby-core:92566] [Ruby trunk Feature#15831] Add `Array#extract!`, `Hash#extract!`, and `ENV::extract!` bogdanvlviv
2019-05-06  6:01 ` [ruby-core:92569] " shevegen
2019-05-14  0:33 ` [ruby-core:92638] [Ruby trunk Feature#15831] Add `Array#extract`, `Hash#extract`, and `ENV::extract` nobu
2019-05-14 21:24 ` [ruby-core:92649] " bogdanvlviv
2019-05-18 14:45 ` [ruby-core:92709] " bogdanvlviv
2019-05-22  7:45 ` matz [this message]
2019-05-22  9:52 ` [ruby-core:92780] [Ruby trunk Feature#15831] Add `Array#extract`, `Hash#extract`, and `ENV.extract` bogdanvlviv
2019-05-24 10:39 ` [ruby-core:92821] " bogdanvlviv

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-78137.20190522074539.9758acdee4e10446@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).