Issue #15831 has been updated by bogdanvlviv (Bogdan Denkovych).


nobu (Nobuyoshi Nakada) wrote:
> Why `Array#extract` has no argument but takes a block, while `Hash` and `ENV` are opposite?


I implemented those methods to mirror similar behavior as it is in Active Support, but I also feel like we need to discuss more about behavior and signature of those methods:

## About `Array#extract`

```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]
```
I do believe that this method should mirror the behavior and the method signature of `Array#reject!` https://docs.ruby-lang.org/en/2.6.0/Array.html#method-i-reject-21 since they are similar except one thing - `Array#extract` returns elements for which the block returns a `true` value. Actually "return elements for the block returns a `true` value" is the main reason why I would like to add this method because there could be and [there already are lots of use cases for those methods](https://bogdanvlviv.com/posts/ruby/rails/array-extract-to-activesupport-6-0.html). So I believe this method good as it is.

## About `Hash#extract` and `ENV::extract`

First of all, thank you for your question. I also feel `Hash#extract`/`ENV::extract` should receive a block, in other words should mirror the behavior and the method signature o`Hash#reject!`, but return the hash with key/value pairs for which the block returns `true`. There is an example to make it clear:
```ruby
h = {a: 100, b: 200, c: 300}
h.extract {|k, v| v > 150} # => {:b=>200, :c=>300}
h # => {:a=>100}
```

I also feel like we should add `Hash#slice!`(since we have `Hash#slice`) that would behave exactly in the way I proposed to `Hash#extract` initially.
```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 # => {}

```
I would like to work on `Hash#slice!` implementation as well.


Let me know what do you think about it.

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

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

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>