Skip to content

Instantly share code, notes, and snippets.

@nickangtc
Last active February 20, 2022 08:37
Show Gist options
  • Save nickangtc/239beea9372d0b9fe55440e782e173c7 to your computer and use it in GitHub Desktop.
Save nickangtc/239beea9372d0b9fe55440e782e173c7 to your computer and use it in GitHub Desktop.
# this example shows that in ruby you can
# define how an object is being compared
# to another object with a custom `==` method
class ProductItem
attr_reader :name, :quantity
def initialize(name, quantity)
@name = name
@quantity = quantity
end
def ==(other)
puts "comparison made between #{self.name} and #{other.name}"
end
end
p1 = ProductItem.new('coffee beans', 12)
p2 = ProductItem.new('aeropress', 5)
p1 == p2
p1.==(p2) # this is the verbose syntax
#=> comparison made between coffee beans and aeropress
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment