Austin Ziegler
10/2/2003 10:38:00 PM
On Fri, 3 Oct 2003 06:14:45 +0900, Greg Vaughn wrote:
> Dimitrios Galanakis wrote:
>> I was expecting an answer like that... I wanted to ask then if operator
>> overloading if exactly similar to function overloading. I would like
>> for example to define my own complex class. In this canse the *
>> operator will perform differencly when multiplying a real and a complex
>> and when multiplying two complex numbers (this is almost what I need to
>> do only the difference is that instead of a complex class I have some
>> other class for which it makes sence to multiply objects of that class
>> together and with real numbers). In other words how can I simplify the
>> following code by removing the ifs?
> I'm still a relative Ruby newbie, so I can't bang out any example code,
> but I hope I can point out a couple of useful tidbits. In Ruby classes
> aren't closed. That means you can add methods to Numeric whenever you
> want. Also, as written, your code wouldn't work if you multiplied Numeric
> * Complex
In this case, what is wanted is Numeric#coerce.
-austin
--
austin ziegler * austin@halostatue.ca * Toronto, ON, Canada
software designer * pragmatic programmer * 2003.10.02
* 18.37.33