Created
October 16, 2013 18:00
-
-
Save alloy-d/7012120 to your computer and use it in GitHub Desktop.
Set logic for Ruby hashes. Somehow nothing like this is built in.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'set' | |
class Hash | |
# Returns true if the set of this Hash's key/value pairs | |
# is a superset of the given hash's key/value pairs. | |
# | |
# See Set#superset? | |
def superhash?(hash) | |
self.to_set.superset?(hash.to_set) | |
end | |
# Returns true if the set of this Hash's key/value pairs | |
# is a proper superset of the given hash's key/value pairs. | |
# | |
# See Set#proper_superset? | |
def proper_superhash?(hash) | |
self.to_set.proper_superset?(hash.to_set) | |
end | |
# Returns true if the set of this Hash's key/value pairs | |
# is a subset of the given hash's key/value pairs. | |
# | |
# See Set#subset? | |
def subhash?(hash) | |
self.to_set.subset?(hash.to_set) | |
end | |
# Returns true if the set of this Hash's key/value pairs | |
# is a proper subset of the given hash's key/value pairs. | |
# | |
# See Set#proper_subset? | |
def proper_subhash?(hash) | |
self.to_set.proper_subset?(hash.to_set) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment