# stripped version of Andrea Fazzi's submission to Ruby Quiz #179 class Modulo include Comparable def initialize(n = 0, m = 13) @n, @m = n % m, m end def to_i @n end def <=>(other_n) @n <=> other_n.to_i end [:+, :-, :*, :**].each do |meth| define_method(meth) { |other_n| Modulo.new(@n.send(meth, other_n.to_i), @m) } end def coerce(numeric) [numeric, @n] end end # Demo x, y = Modulo.new(10), Modulo.new(20) p x > y # true p x == y # false p [x,y].sort #[#, #] p x + y ## p 2 + y # 9 p y + 2 ## p x**100 + x +1 ##