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


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

Since proposed `Hash#slice!` method is the same as `Hash#extract!` from the Active Support, there is one use-case where we have some `options` then we extract some known by key/s options and assign it to `path_options` variable to be able to use them later in one way and with the rest `options` to use in another way: 
https://github.com/rails/rails/blob/c1e949e9e618f75dc446ffa584c3b441c48714b1/actionview/lib/action_view/helpers/asset_tag_helper.rb#L89

----------------------------------------
Feature #15863: Add `Hash#slice!` and `ENV.slice!`
https://bugs.ruby-lang.org/issues/15863#change-78156

* Author: bogdanvlviv (Bogdan Denkovych)
* Status: Rejected
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
## Add `Hash#slice!`

In https://bugs.ruby-lang.org/issues/8499 we added `Hash#slice`.

`Hash#slice!` removes and returns the key/value pairs matching the given keys:

```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 # => {}
```

Note that, this method reflects the behavior of Active Support's
`Hash#extract!` method that was added in 2009, see
https://github.com/rails/rails/commit/8dcf91ca113579646e95b0fd7a864dfb6512a53b

```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 # => {}
```
(There is a proposal to add `Hash#extract` to Ruby - https://bugs.ruby-lang.org/issues/15831, but it has another method signature)

Active Support also has `Hash#slice!`, see https://api.rubyonrails.org/v5.2/classes/Hash.html#method-i-slice-21.
It is quite different what this patch proposes, see how it works:

```ruby
h = {a: 100, b: 200, c: 300}
h.slice!(:a) # => {:b=>200, :c=>300} # AS Hash#slice!
h # => {:a=>100}
h.slice!(:b, :c, :d) # => {:a=>100} # AS Hash#slice!
h # => {}
```

I think `Hash#slice!` in Ruby should work in the same way as `Hash#extract!` from Active Support,
there is one argument:
- https://bugs.ruby-lang.org/issues/8499#note-31
  It should behave in the way `Hash#slice` does, except one thing
  `Hash#slice!` modifies the object. (See, for instance, how `Array#slice` and
  `Array#slice!` work, they return the same value)

But I would like to discuss it more to choose the right behavior for the
proposed method. (Maybe there are good arguments why we should add `Hash#slice!`
with behavior as it is in Active Support)


## Add `ENV.slice!`

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

```ruby
ENV.slice!("PORT", "RAILS_ENV") # => {"PORT"=>"3000", "RAILS_ENV"=>"development"}
```


Pull Request: https://github.com/ruby/ruby/pull/2195
Patch: https://patch-diff.githubusercontent.com/raw/ruby/ruby/pull/2195.patch



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