From: Gregory Brown Date: 2008-10-14T02:14:58+09:00 Subject: Re: [QUIZ] Modular Arithmetic (#179) On Fri, Oct 10, 2008 at 11:13 AM, Matthew Moss wrote: > ## Modular Arithmetic (#179) Doesn't handle division, may have some other issues, but more-or-less works. -greg class Modulo include Comparable def initialize(num, mod=26) @num = num @mod = mod end def to_i @num % @mod end def coerce(other) [other,to_i] end def <=>(other) to_i <=> other.to_i end def self.modulo_operators(*ops) ops.each do |op| define_method(op) { |other| Modulo.new(@num.send(op, other.to_i) % @mod) } end end def self.modulo_delegates(*msgs) msgs.each { |msg| define_method(msg) { |*args| to_i.send(msg, *args) } } end modulo_operators :+, :-, :*, :** modulo_delegates :to_s end -- Technical Blaag at: http://blog.majesticseacreature.com | Non-tech stuff at: http://metametta.blogspot.com