Issue #8499 has been updated by Keith Bennett.


I was about to report this feature request but was happy to find that it was already there...but disappointed that it's been here so long. I'm using Ruby but not Rails and have been needing this functionality a lot lately.

Unless I'm misunderstanding, the implementation of such methods would be trivial.  I would much rather see this implemented with a nonoptimal name than not implemented at all.

'slice' and 'except' are ok with me, but if there is a concern about their correctness as names, perhaps they could be 'select_if_key_in' and 'reject_if_key_in'.

But again, anything would be better than nothing.

----------------------------------------
Feature #8499: Importing Hash#slice, Hash#slice!, Hash#except, and Hash#except! from ActiveSupport
https://bugs.ruby-lang.org/issues/8499#change-54753

* Author: Kenta Murata
* Status: Assigned
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
According to my experiences, the following two idioms often appeare in application codes.

1. `hash = other_hash.select { |k, | [:key1, :key2, :key3].include? k }`
2. `hash = other_hash.reject { |k, | [:key1, :key2, :key3].include? k }`

On Rails, they can be written in the following forms by using ActiveSupport's features.

1. `hash = other_hash.slice(:key1, :key2, :key3)`
2. `hash = other_hash.except(:key1, :key2, :key3)`

I think the latter forms are shorter and more readable than the former ones.

So I propose to import the following methods from ActiveSupport:

* `Hash#slice`
* `Hash#slice!`
* `Hash#except`
* `Hash#except!`


---Files--------------------------------
patch.diff (4.05 KB)
patch2.diff (4.07 KB)


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