From: "sawa (Tsuyoshi Sawada)" <noreply@...>
Date: 2021-12-08T05:16:06+00:00
Subject: [ruby-core:106545] [Ruby master Feature#18395] Introduce Array#subtract! for performance

Issue #18395 has been updated by sawa (Tsuyoshi Sawada).


I am strongly against it. It is a practice in Ruby for methods that modify the receiver by removing something to return the removed value rather than the modified receiver.

We have `Array#delete` already, the only difference from your proposal being that it does not accept multiple arguments. I think a better proposal would be to let `Array#delete` accept multiple arguments.

```ruby
a = [1, 2, 3, 2, 1]
b = [4, 2, 3]

a.delete(*b) #=> 2
a # => [1, 1]
```

----------------------------------------
Feature #18395: Introduce Array#subtract! for performance 
https://bugs.ruby-lang.org/issues/18395#change-95209

* Author: schneems (Richard Schneeman)
* Status: Open
* Priority: Normal
----------------------------------------
PR: https://github.com/ruby/ruby/pull/5110#issuecomment-984345309

It is common to use `-=` to modify an array to remove elements. Also, there is `Array#difference` which accepts multiple arguments. Both of these methods allocate an intermediate array when the original array could be re-used.

I am proposing we add an API onto Array that allows the programmer to perform a subtraction/difference operation and mutate the original array.

I am proposing `Array#subtract!`. Reasons why I did not choose `Array#difference!` are discussed in the comments of the PR. I'm also happy to discuss alternative names.



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