10 Things Every Java Programmer Should Know About Ruby | [ Prev | Home | Next ] |
A mix-in allows the commonality to be factored out.
module LessComparable
def >(other)
other < self
end
# Other methods defined in terms of less than:
# <=, >=, ==
end
class Pair
include LessComparable
attr_accessor :first, :second
# ...
def <(other)
(first < other.first) ||
(first == other.first && second < other.second)
end
end
O'REILLY® OSCON 2005 | Copyright 2005 by Jim Weirich (All Rights Reserved) |