ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:92566] [Ruby trunk Feature#15831] Add `Array#extract!`, `Hash#extract!`, and `ENV::extract!`
       [not found] <redmine.issue-15831.20190505205201@ruby-lang.org>
@ 2019-05-05 20:52 ` bogdanvlviv
  2019-05-06  6:01 ` [ruby-core:92569] " shevegen
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 8+ messages in thread
From: bogdanvlviv @ 2019-05-05 20:52 UTC (permalink / raw)
  To: ruby-core

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

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

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

There is a benchmark script:

```ruby
require "benchmark"

class Array
  def activesupport_extract!
    return to_enum(:activesupport_extract!) { size } unless block_given?

    extracted_elements = []

    reject! do |element|
      extracted_elements << element if yield(element)
    end

    extracted_elements
  end
end

arrays_for_partition = Array.new(1000) { (0..10000).to_a }
arrays_for_extract = Array.new(1000) { (0..10000).to_a }
arrays_for_activesupport_extract = Array.new(1000) { (0..10000).to_a }

Benchmark.bmbm do |x|
  x.report("Array#partition")  do
    arrays_for_partition.each do |numbers|
      odd_numbers, numbers = numbers.partition { |number| number.odd? }
      numbers
    end
  end

  x.report("Array#extract!")  do
    arrays_for_extract.each do |numbers|
      odd_numbers = numbers.extract! { |number| number.odd? }
      numbers
    end
  end

  x.report("Array#activesupport_extract!")  do
    arrays_for_activesupport_extract.each do |numbers|
      odd_numbers = numbers.activesupport_extract! { |number| number.odd? }
      numbers
    end
  end
end

and its result:

```bash
Rehearsal ----------------------------------------------------------------
Array#partition                0.657710   0.003571   0.661281 (  0.662462)
Array#extract!                 0.509381   0.002581   0.511962 (  0.513105)
Array#activesupport_extract!   0.811371   0.000000   0.811371 (  0.812456)
------------------------------------------------------- total: 1.984614sec

                                   user     system      total        real
Array#partition                0.623502   0.000000   0.623502 (  0.625004)
Array#extract!                 0.193920   0.000000   0.193920 (  0.194283)
Array#activesupport_extract!   0.308468   0.000000   0.308468 (  0.309037)
```

#################################################################################

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 in 2009, see
https://github.com/rails/rails/commit/8dcf91ca113579646e95b0fd7a864dfb6512a53b

There is a benchmark script:

```ruby
require "benchmark"

class Hash
  def activesupport_extract!(*keys)
    keys.each_with_object(self.class.new) { |key, result| result[key] = delete(key) if has_key?(key) }
  end
end

Benchmark.bmbm do |x|
  x.report("Hash#extract")  do
    10000.times do
      hash_for_extract =  { a: 1, b: 2, c: 3, d: 4 }
      executed = hash_for_extract.extract!(:a, :b, :d, :x, :y)
    end
  end

  x.report("Hash#activesupport_extract!")  do
    10000.times do
      hash_for_activesupport_extract =  { a: 1, b: 2, c: 3, d: 4 }
      executed = hash_for_activesupport_extract.activesupport_extract!(:a, :b, :d, :x, :y)
    end
  end
end
```

and its result:

```bash
Rehearsal ---------------------------------------------------------------
Hash#extract                  0.004724   0.000000   0.004724 (  0.004670)
Hash#activesupport_extract!   0.005686   0.004434   0.010120 (  0.010248)
------------------------------------------------------ total: 0.014844sec

                                  user     system      total        real
Hash#extract                  0.002562   0.000000   0.002562 (  0.002715)
Hash#activesupport_extract!   0.009864   0.000000   0.009864 (  0.009997)
```

#################################################################################

Add `ENV::extract!`

The method removes and returns the key/value pairs matching the given keys.

```ruby
ENV.extract!("TERM","HOME") # => {"TERM"=>"xterm-256color", "HOME"=>"/Users/rhc"}
```

Pull Request: https://github.com/ruby/ruby/pull/2171



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [ruby-core:92569] [Ruby trunk Feature#15831] Add `Array#extract!`, `Hash#extract!`, and `ENV::extract!`
       [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 ` shevegen
  2019-05-14  0:33 ` [ruby-core:92638] [Ruby trunk Feature#15831] Add `Array#extract`, `Hash#extract`, and `ENV::extract` nobu
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 8+ messages in thread
From: shevegen @ 2019-05-06  6:01 UTC (permalink / raw)
  To: ruby-core

Issue #15831 has been updated by shevegen (Robert A. Heiler).


What is the difference towards e. g. hash.delete()?

People may ask about this difference. The '!'
too since you can use e. g. Hash #delete as-is.

Also without '!', I am not sure if extract is a good
name per se, but these are just random comments mostly,
you only have to convince matz, not me. :)

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

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

There is a benchmark script:

```ruby
require "benchmark"

class Array
  def activesupport_extract!
    return to_enum(:activesupport_extract!) { size } unless block_given?

    extracted_elements = []

    reject! do |element|
      extracted_elements << element if yield(element)
    end

    extracted_elements
  end
end

arrays_for_partition = Array.new(1000) { (0..10000).to_a }
arrays_for_extract = Array.new(1000) { (0..10000).to_a }
arrays_for_activesupport_extract = Array.new(1000) { (0..10000).to_a }

Benchmark.bmbm do |x|
  x.report("Array#partition")  do
    arrays_for_partition.each do |numbers|
      odd_numbers, numbers = numbers.partition { |number| number.odd? }
      numbers
    end
  end

  x.report("Array#extract!")  do
    arrays_for_extract.each do |numbers|
      odd_numbers = numbers.extract! { |number| number.odd? }
      numbers
    end
  end

  x.report("Array#activesupport_extract!")  do
    arrays_for_activesupport_extract.each do |numbers|
      odd_numbers = numbers.activesupport_extract! { |number| number.odd? }
      numbers
    end
  end
end

and its result:

```bash
Rehearsal ----------------------------------------------------------------
Array#partition                0.657710   0.003571   0.661281 (  0.662462)
Array#extract!                 0.509381   0.002581   0.511962 (  0.513105)
Array#activesupport_extract!   0.811371   0.000000   0.811371 (  0.812456)
------------------------------------------------------- total: 1.984614sec

                                   user     system      total        real
Array#partition                0.623502   0.000000   0.623502 (  0.625004)
Array#extract!                 0.193920   0.000000   0.193920 (  0.194283)
Array#activesupport_extract!   0.308468   0.000000   0.308468 (  0.309037)
```

#################################################################################

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 in 2009, see
https://github.com/rails/rails/commit/8dcf91ca113579646e95b0fd7a864dfb6512a53b

There is a benchmark script:

```ruby
require "benchmark"

class Hash
  def activesupport_extract!(*keys)
    keys.each_with_object(self.class.new) { |key, result| result[key] = delete(key) if has_key?(key) }
  end
end

Benchmark.bmbm do |x|
  x.report("Hash#extract")  do
    10000.times do
      hash_for_extract =  { a: 1, b: 2, c: 3, d: 4 }
      executed = hash_for_extract.extract!(:a, :b, :d, :x, :y)
    end
  end

  x.report("Hash#activesupport_extract!")  do
    10000.times do
      hash_for_activesupport_extract =  { a: 1, b: 2, c: 3, d: 4 }
      executed = hash_for_activesupport_extract.activesupport_extract!(:a, :b, :d, :x, :y)
    end
  end
end
```

and its result:

```bash
Rehearsal ---------------------------------------------------------------
Hash#extract                  0.004724   0.000000   0.004724 (  0.004670)
Hash#activesupport_extract!   0.005686   0.004434   0.010120 (  0.010248)
------------------------------------------------------ total: 0.014844sec

                                  user     system      total        real
Hash#extract                  0.002562   0.000000   0.002562 (  0.002715)
Hash#activesupport_extract!   0.009864   0.000000   0.009864 (  0.009997)
```

#################################################################################

Add `ENV::extract!`

The method removes and returns the key/value pairs matching the given keys.

```ruby
ENV.extract!("TERM","HOME") # => {"TERM"=>"xterm-256color", "HOME"=>"/Users/rhc"}
```

Pull Request: https://github.com/ruby/ruby/pull/2171



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [ruby-core:92638] [Ruby trunk Feature#15831] Add `Array#extract`, `Hash#extract`, and `ENV::extract`
       [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 ` nobu
  2019-05-14 21:24 ` [ruby-core:92649] " bogdanvlviv
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 8+ messages in thread
From: nobu @ 2019-05-14  0:33 UTC (permalink / raw)
  To: 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/

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [ruby-core:92649] [Ruby trunk Feature#15831] Add `Array#extract`, `Hash#extract`, and `ENV::extract`
       [not found] <redmine.issue-15831.20190505205201@ruby-lang.org>
                   ` (2 preceding siblings ...)
  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 ` bogdanvlviv
  2019-05-18 14:45 ` [ruby-core:92709] " bogdanvlviv
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 8+ messages in thread
From: bogdanvlviv @ 2019-05-14 21:24 UTC (permalink / raw)
  To: ruby-core

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/

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [ruby-core:92709] [Ruby trunk Feature#15831] Add `Array#extract`, `Hash#extract`, and `ENV::extract`
       [not found] <redmine.issue-15831.20190505205201@ruby-lang.org>
                   ` (3 preceding siblings ...)
  2019-05-14 21:24 ` [ruby-core:92649] " bogdanvlviv
@ 2019-05-18 14:45 ` bogdanvlviv
  2019-05-22  7:45 ` [ruby-core:92772] [Ruby trunk Feature#15831] Add `Array#extract`, `Hash#extract`, and `ENV.extract` matz
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 8+ messages in thread
From: bogdanvlviv @ 2019-05-18 14:45 UTC (permalink / raw)
  To: ruby-core

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

Description updated

I just changed the implementation of `Hash#extract` and `ENV::extract` as it's described in the previous note https://bugs.ruby-lang.org/issues/15831#note-4

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

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



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [ruby-core:92772] [Ruby trunk Feature#15831] Add `Array#extract`, `Hash#extract`, and `ENV.extract`
       [not found] <redmine.issue-15831.20190505205201@ruby-lang.org>
                   ` (4 preceding siblings ...)
  2019-05-18 14:45 ` [ruby-core:92709] " bogdanvlviv
@ 2019-05-22  7:45 ` matz
  2019-05-22  9:52 ` [ruby-core:92780] " bogdanvlviv
  2019-05-24 10:39 ` [ruby-core:92821] " bogdanvlviv
  7 siblings, 0 replies; 8+ messages in thread
From: matz @ 2019-05-22  7:45 UTC (permalink / raw)
  To: ruby-core

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/

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [ruby-core:92780] [Ruby trunk Feature#15831] Add `Array#extract`, `Hash#extract`, and `ENV.extract`
       [not found] <redmine.issue-15831.20190505205201@ruby-lang.org>
                   ` (5 preceding siblings ...)
  2019-05-22  7:45 ` [ruby-core:92772] [Ruby trunk Feature#15831] Add `Array#extract`, `Hash#extract`, and `ENV.extract` matz
@ 2019-05-22  9:52 ` bogdanvlviv
  2019-05-24 10:39 ` [ruby-core:92821] " bogdanvlviv
  7 siblings, 0 replies; 8+ messages in thread
From: bogdanvlviv @ 2019-05-22  9:52 UTC (permalink / raw)
  To: 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/

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [ruby-core:92821] [Ruby trunk Feature#15831] Add `Array#extract`, `Hash#extract`, and `ENV.extract`
       [not found] <redmine.issue-15831.20190505205201@ruby-lang.org>
                   ` (6 preceding siblings ...)
  2019-05-22  9:52 ` [ruby-core:92780] " bogdanvlviv
@ 2019-05-24 10:39 ` bogdanvlviv
  7 siblings, 0 replies; 8+ messages in thread
From: bogdanvlviv @ 2019-05-24 10:39 UTC (permalink / raw)
  To: ruby-core

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


> I also concern about the name conflict with the ActiveSupport method.

I guess this comment addressed https://bugs.ruby-lang.org/issues/15863 that adds `Hash#slice!`, right?
Active Support doesn't have any `extract` methods, only `extract!`(with bang).

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

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

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2019-05-24 10:40 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [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 ` [ruby-core:92772] [Ruby trunk Feature#15831] Add `Array#extract`, `Hash#extract`, and `ENV.extract` matz
2019-05-22  9:52 ` [ruby-core:92780] " bogdanvlviv
2019-05-24 10:39 ` [ruby-core:92821] " bogdanvlviv

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).