From: "Gray, Jeff" Date: 2002-05-11T05:08:57+09:00 Subject: RE: Reversing receiver and argument for commutative math operatio ns > |Let's say I have a class that overrides certain arithmetic > |operators like + > |and *, so that > | > | obj = MyClass.new(5, "some other attribute") > | this_obj = obj * 2 # this_obj == MyClass.new(10) > | that_obj = 2 * obj # that_obj == this_obj is desired > > |Is there a way to do what I'd like? > > How about > > class MyClass > def *(x) > if x.type <= self.type > x = x.value > end > self.type.new(self.value * x) > end > def coerce(x) > [self.type.new(x), self] > end > end > matz. D'oh! I was so close, but botched the definition of MyClass#*, causing the infinite loop. Thanks!