From: "Glass_saga (Masaki Matsushita)" Date: 2013-07-29T12:24:46+09:00 Subject: [ruby-core:56252] [ruby-trunk - Feature #8499] Importing Hash#slice, Hash#slice!, Hash#except, and Hash#except! from ActiveSupport Issue #8499 has been updated by Glass_saga (Masaki Matsushita). File patch2.diff added I'm sorry for my wrong implementation. patch2.diff makes Hash#slice! and #except! behave the same as ActiveSupport. However, I think it isn't good idea to import Hash#slice! and #except! from ActiveSupport as it is. Because: * They returns self if no changes were made. It is inconsistent with other built-in methods like #select! and #reject!. * #slice! returns rest of hash, not slice of hash like following. It may be confusing. hash = {1=>2,3=>4,5=>6} hash.slice!(1,3) #=> {5,6} hash #=> {1=>2,3=>4} ---------------------------------------- Feature #8499: Importing Hash#slice, Hash#slice!, Hash#except, and Hash#except! from ActiveSupport https://bugs.ruby-lang.org/issues/8499#change-40745 Author: mrkn (Kenta Murata) Status: Assigned Priority: Normal Assignee: matz (Yukihiro Matsumoto) Category: core Target version: current: 2.1.0 =begin RD 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!})) =end -- http://bugs.ruby-lang.org/