Sha256: c34b27018b1b30116a082b7abce96e1ae55295e7f4fe86e1f095918bae24d720
Contents?: true
Size: 988 Bytes
Versions: 7
Compression:
Stored size: 988 Bytes
Contents
# It would nice if we could the following. Then the middle portion of the #Comparable # method would not be needed. But I fear it might break others code. # # module Comparable # # def <=>(other) # comparability.each do |field| # cmp = send(field) <=> other.send(field); return cmp unless cmp == 0 # end # end # # end class Module # Automatically generate sorting defintions base on attribute fields. # # include SortOn(:a, :b) # # is equivalent to including a module containing: # # def <=>(other) # cmp = self.a <=> other.a; return cmp unless cmp == 0 # cmp = self.b <=> other.b; return cmp unless cmp == 0 # 0 # end def Comparable(*accessors) define_method(:comparability){ accessors } code = %{ def <=>(other) comparability.each do |a| cmp = (send(a) <=> other.send(a)); return cmp unless cmp == 0 end end } module_eval code return Comparable end end
Version data entries
7 entries across 7 versions & 2 rubygems