Data Structures and Algorithms with Object-Oriented Design Patterns in Ruby
next up previous index

Operator Overloading

Program gif illustrates operator overloading in Ruby. Operator overloading   allows the programmer to use the built-in operators for user-defined types.

   program56326
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 * e
is equivalent in its effect to invoking the methods as follows
c.+(d.*(e))


next up previous index

Bruno Copyright © 2004 by Bruno R. Preiss, P.Eng. All rights reserved.