From: ts Date: 2004-05-02T20:43:20+09:00 Subject: Re: Commutativity of '*' >>>>> "S" == =?ISO-8859-1?Q?Stephan K=E4mper?= writes: S> # But this won't. Instead I get: S> # .../lm.rb:25:in `*': Foo can't be coerced into Float (TypeError) Well, ruby is saying that it can coerce, learn it how to do it One way to do it svg% cat b.rb #!/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