Skip to content

Instantly share code, notes, and snippets.

View denisdefreyne's full-sized avatar

Denis Defreyne denisdefreyne

View GitHub Profile
class Employee
# …
def ==(other)
super || (
self.class == other.class &&
[email protected]? &&
@id == other.id
)
end
end
class Employee
attr_reader :id
attr_reader :name
def initialize(id, name)
@id = id
@name = name
end
end
class Point
# …
def ==(other)
self.class == other.class &&
@x == other.x &&
@y == other.y
end
end
Float::NAN == Float::NAN # => false
a != b # => true
a != "rabbit" # => true
a = Point.new(4, 6)
b = Point.new(3, 7)
a_also = Point.new(4, 6)
a == a # => true
a == b # => false
a == "soup" # => false
a == a_also # => true
class Point
# …
def ==(other)
self.class == other.class &&
@x == other.x &&
@y == other.y
end
end
a = Point.new(4, 6)
b = Point.new(3, 7)
a_also = Point.new(4, 6)
a == a # => true
a == b # => false
a == "soup" # => false
a == a_also # => false?!
class Point
attr_reader :x
attr_reader :y
def initialize(x, y)
@x = x
@y = y
end
end
geb = Book.find_by(title: "Gödel, Escher, Bach")
also_geb = Book.find_by(isbn: "978-0-14-028920-6")
geb == also_geb # => true