|
Data Structures and Algorithms
with Object-Oriented Design Patterns in Ruby |
Program
illustrates operator overloading in Ruby.
Operator overloading
allows the programmer to use the built-in operators for user-defined types.

Program: Complex class +, -, * and / methods.
To overload the built-in +, -, * and / operators so that they may be used with Complex operands, we define the methods called +, -, * and / (respectively). Given Complex variables c, d and e, the expression
c + d * eis equivalent in its effect to invoking the methods as follows
c.+(d.*(e))