Project

General

Profile

Feature #8499

Importing Hash#slice, Hash#slice!, Hash#except, and Hash#except! from ActiveSupport

Added by mrkn (Kenta Murata) over 7 years ago. Updated about 3 years ago.

Status:
Closed
Priority:
Normal
Target version:
[ruby-core:55330]

Description

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) patch.diff built-in Hash#slice, Hash#slice!, Hash#except, and Hash#except! Glass_saga (Masaki Matsushita), 07/26/2013 10:58 PM
patch2.diff (4.07 KB) patch2.diff Glass_saga (Masaki Matsushita), 07/29/2013 12:24 PM
patch3.diff (4.04 KB) patch3.diff Glass_saga (Masaki Matsushita), 09/07/2017 10:09 AM
patch4.diff (4.5 KB) patch4.diff Glass_saga (Masaki Matsushita), 09/19/2017 05:41 AM

Related issues

Related to Ruby master - Feature #9108: Hash sub-selectionsClosedmatz (Yukihiro Matsumoto)Actions
Related to Ruby master - Feature #12461: Hash & keys to make subset.RejectedActions
Related to Ruby master - Feature #13563: Implement Hash#choice method.ClosedActions
Related to Ruby master - Feature #15863: Add `Hash#slice!` and `ENV.slice!`RejectedActions
Has duplicate Ruby master - Feature #15822: Add Hash#exceptClosedActions

Also available in: Atom PDF