From: matz@... Date: 2016-07-19T07:50:58+00:00 Subject: [ruby-core:76423] [Ruby trunk Bug#12461][Closed] Hash & keys to make subset. Issue #12461 has been updated by Yukihiro Matsumoto. Status changed from Open to Closed I am against having `Hash#&`. `Array#&` takes two arrays. The proposed `Hash#&` takes a hash and key array. if you want `Hash#slice` to be added to the core, submit a new issue. But note that now we have real keyword arguments so that we have less need for manipulating keyword hashes explicitly. Matz. ---------------------------------------- Bug #12461: Hash & keys to make subset. https://bugs.ruby-lang.org/issues/12461#change-59666 * Author: Samuel Williams * Status: Closed * Priority: Normal * Assignee: * ruby -v: * Backport: 2.1: UNKNOWN, 2.2: UNKNOWN, 2.3: UNKNOWN ---------------------------------------- It would be nice if Hash had a way to create a sub-hash with a limited set of keys. This would be useful for keyword functions in particular, because sometimes you have a large hash of things and you only want to send a subset to another function. A definition somewhat like: class Hash def &(keys) keys.each_with_object(self.class.new) {|key, hash| hash[key] = self[key] if has_key?(key)} end end You can see some similar definitions here: https://github.com/rails/rails/blob/b670fadb978c8a12c3414ed842cd49e4fde2cec0/activesupport/lib/active_support/core_ext/hash/slice.rb#L21 It might be nice to support `slice` and `slice!` in a similar logical sense to `Array` rather than proposed `&` operator above - that would just be nice shortcut. -- https://bugs.ruby-lang.org/ Unsubscribe: