Imagine a class including comparable like this:
class Element
include Comparable
attr_accessor :name, :pos_x, :pos_y
def initialize(name, pos_x, pos_y)
@name = name
@pos_x = pos_x
@pos_y = pos_y
end
def <=>(other)
if (@pos_x == other.pos_x) and (@pos_y == other.pos_y)
return 0
else
return @name <=> other.name
end
end
def eql?(other)
self == other
end
end
How would you implement the hash
function such that a.hash == b.hash
in this case? In general I'd do:
def hash
@name.hash
end
But this does not include pos_x
and pos_y
.