Re: Modular Arithmetic (#179)
- From: toomln <micathom@xxxxxxxxx>
- Date: Sun, 12 Oct 2008 11:55:20 -0700 (PDT)
## Modular Arithmetic (#179)
Here is a (maybe too) simple proxy solution. Modular division isn't
implemented. It passes the tests -- little more.
This solution is for ruby 1.9 only. AFAIK facets also has a
BasicObject class that could (or maybe not) be used as a replacement
for ruby19's BasicObject.
Regards,
Thomas.
#!/usr/bin/env ruby19
class Modulo < BasicObject
def initialize(num, modulo=26)
@modulo = modulo
@num = num % @modulo
end
def method_missing(meth, *args)
rv = @num.send(meth, *args)
case rv
when ::Numeric
::Modulo.new(rv % @modulo)
else
rv
end
end
def <=>(other)
@num <=> other.real
end
def ==(other)
@num == other.real
end
alias :equal? :==
def real
@num
end
end
.
- References:
- [QUIZ] Modular Arithmetic (#179)
- From: Matthew Moss
- [QUIZ] Modular Arithmetic (#179)
- Prev by Date: [ANN] ffmpeg-ruby first release : Now extracting thumbnails
- Next by Date: Re: Updating Ruby 1.8.6 to 1.8.6-p287 in Debian 4
- Previous by thread: [QUIZ][SOLUTION] Modular Arithmetic (#179)
- Next by thread: Re: [QUIZ] Modular Arithmetic (#179)
- Index(es):
Relevant Pages
|
Loading