Last active
February 20, 2022 08:37
-
-
Save nickangtc/239beea9372d0b9fe55440e782e173c7 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
# 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