From: knu@... Date: 2017-10-19T07:28:55+00:00 Subject: [ruby-core:83381] [Ruby trunk Feature#8499] Importing Hash#slice, Hash#slice!, Hash#except, and Hash#except! from ActiveSupport Issue #8499 has been updated by knu (Akinori MUSHA). One concern is that Array#slice! returns deleted elements whereas this Hash#slice! would return pairs left after slicing. ---------------------------------------- Feature #8499: Importing Hash#slice, Hash#slice!, Hash#except, and Hash#except! from ActiveSupport https://bugs.ruby-lang.org/issues/8499#change-67342 * Author: mrkn (Kenta Murata) * Status: Assigned * Priority: Normal * Assignee: matz (Yukihiro Matsumoto) * Target version: 2.5 ---------------------------------------- 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) patch3.diff (4.04 KB) patch4.diff (4.5 KB) -- 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>