Skip to content

Instantly share code, notes, and snippets.

@baweaver
Created September 10, 2019 02:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baweaver/70cdeee5170f4a1b48b6b8c83858a498 to your computer and use it in GitHub Desktop.
Save baweaver/70cdeee5170f4a1b48b6b8c83858a498 to your computer and use it in GitHub Desktop.
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