10 Things Every Java Programmer Should Know About Ruby | [ Prev | Home | Next ] |
Although all the logic is in the less-than method, all the other comparisons must still be defined.
class Pair
attr_accessor :first, :second
# ...
def <(other)
(first < other.first) ||
(first == other.first && second < other.second)
end
def >(other)
other < self
end
# Other methods defined in terms of less than:
# <=, >=, ==
end
O'REILLY® OSCON 2005 | Copyright 2005 by Jim Weirich (All Rights Reserved) |