Project

General

Profile

Actions

Feature #12461

closed

Hash & keys to make subset.

Added by ioquatix (Samuel Williams) over 5 years ago. Updated about 5 years ago.

Status:
Rejected
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:75851]

Description

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.


Related issues

Related to Ruby master - Feature #8499: Importing Hash#slice, Hash#slice!, Hash#except, and Hash#except! from ActiveSupportClosedmatz (Yukihiro Matsumoto)Actions
Actions

Also available in: Atom PDF