3
# Automatically generate comparitive definitions based on
6
# include Comparable[:a, :b]
8
# is equivalent to including a module containing:
11
# cmp = self.a <=> other.a; return cmp unless cmp == 0
12
# cmp = self.b <=> other.b; return cmp unless cmp == 0
16
def self.[](*accessors)
19
define_method(:comparability){ accessors }
20
define_method(:<=>) do |other|
21
comparability.each do |a|
22
cmp = (send(a) <=> other.send(a))
23
break cmp unless cmp == 0
31
# Would it be nice if we could define comparability for all objects?
32
# Then Comparable[] method would not be needed. Just:
37
# comparability.each do |field|
38
# cmp = send(field) <=> other.send(field); return cmp unless cmp == 0
44
# But I fear it might break other code.