Skip to content

Instantly share code, notes, and snippets.

@alloy-d
Created October 16, 2013 18:00
Show Gist options
  • Save alloy-d/7012120 to your computer and use it in GitHub Desktop.
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.
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