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