Skip to content

Instantly share code, notes, and snippets.

@remogatto
Created December 6, 2011 18:00
Show Gist options
  • Save remogatto/1439190 to your computer and use it in GitHub Desktop.
Save remogatto/1439190 to your computer and use it in GitHub Desktop.
class Modulo
include Comparable
[:+, :-, :*, :**].each do |meth|
define_method(meth) { |other_n| Modulo.new(@n.send(meth, other_n.to_i), @m) }
end
def initialize(n = 0, m = 26)
@n, @m = n % m, m
end
def <=>(other_n)
@n <=> other_n.to_i
end
def to_i
@n
end
private
def coerce(numeric)
[numeric, @n]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment