Created
September 10, 2019 02:32
-
-
Save baweaver/70cdeee5170f4a1b48b6b8c83858a498 to your computer and use it in GitHub Desktop.
This file contains 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
class Hash | |
alias_method :+, :merge | |
def -(other) | |
other_keys = Set.new(other.keys) | |
# Irrelevant value method | |
# reject { |k, _v| other_keys.include?(k) } | |
# Relevant value method | |
reject { |k, v| other_keys.include?(k) && other[k] == v } | |
end | |
def intersection(other) | |
intersecting_keys = self.keys & other.keys | |
# Irrelevant value method | |
# self.slice(*intersecting_keys) | |
# Relevant value method | |
select { |k, v| intersecting_keys.include?(k) && other[k] == v } | |
end | |
alias_method :&, :intersection | |
def exclusive_intersection(other) | |
# Does potentially odd things when values are relevant | |
(self + other) - (self & other) | |
end | |
alias_method :^, :exclusive_intersection | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment