From: Kristof Bastiaensen Date: 2004-05-03T04:33:58+09:00 Subject: Re: Commutativity of '*' On Sun, 02 May 2004 20:43:20 +0900, ts wrote: > #!/usr/bin/ruby > def some_operation_using( lft, rgt ) > "#{lft} times #{rgt}" > end > > class Foo > > def *(other) > # do something > res = some_operation_using( other, self ) > res > end > > def coerce(other) > case other > when Numeric > [self, other] > else > raise TypeError > end > end > > def to_s > "a Foo thingy." > end > > end > > f = Foo.new > puts f * 42.0 > puts 42.0 * f > svg% > > svg% b.rb > 42.0 times a Foo thingy. > 42.0 times a Foo thingy. > svg% > > Guy Decoux It is maybe not a good idea to reverse the order in coerce. It may work with * and +, but you will get a problem when order matters, like in / (division): #--- including the above code --- class Foo def /(other) "divide #{self} by #{other}" end end puts (f / 2) => "divide a Foo thingy. by 2" puts (2 / f) => "divide a Foo thingy. by 2" #--- end code --- Note that some classes which use *, and don't represent a number just give an error: "string" * 2 => "stringstring" 2 * "string" => TypeError: String can't be coerced into Fixnum That behaviour is probably the best if you are not representing a number. Kristof