Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.