On Fri, Oct 10, 2008 at 11:13 AM, Matthew Moss <matt / moss.name> 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