From: ts Date: 2001-01-23T19:43:35+09:00 Subject: [ruby-talk:9761] Re: 101 Misconceptions About Dynamic Languages >>>>> "C" == Christoph Rippel writes: C> class A C> N = 13 # this is the crucial part The problem here is that N is resolved at compile time C> def initialize n C> @rep = n % N C> end C> def + (r) C> (@rep + rep.r) % N ^^^^^ r.rep no ? C> end C> attr_reader :rep C> ... C> end C> The import thing is that the type information is attached to the object ... C> a1 = A.new 11 C> a2 = B.new 11 C> b1 = C.new 10 # definitely want A.id == C.id C> b2 = B.new 11 # is nor hard but is not natively supported C> than the internal representation of C> a1 + b1 and a2 + b2 C> won't be equal. Probably this not what you want (I know nothing about math :-)) pigeon% cat b.rb #!/usr/bin/ruby class Ring @@class = {} def Ring.mod_ZZ(n) return @@class[n] if @@class.key? n a = Class.new(self) a.module_eval "N = #{n}" @@class[n] = a a end def initialize( n) @rep = n % self.class::N end def + (r) (@rep + r.rep) % self.class::N end attr_reader :rep end A = Ring.mod_ZZ 13 B = Ring.mod_ZZ 23 C = Ring.mod_ZZ 13 a1 = A.new 11 a2 = B.new 11 b1 = C.new 10 p A.id == C.id b2 = B.new 11 p a1 + b1 p a2 + b2 pigeon% pigeon% b.rb true 8 22 pigeon% Guy Decoux