Ruby allows us to overload operators so that we can perform operations such as adding two objects together.
Let’s say we have a class Shape, which has width and height properties. We want to be able to add together two Shape objects and, as a result, get a new object that has its width and height equal to the sum of the corresponding properties of the objects.
All we need to do is define the corresponding operator as a method:
attr_accessor :h, :w
def initialize(h, w)
self.h = h
self.w = w
a = Shape.new(7, 4)
b = Shape.new(9, 18)
c = a+b
puts c.h # outputs 16
puts c.w # outputs 22
As you can see, the + method takes one argument, which is another Shape object, and returns a new Shape object with the corresponding values.
CLICK ON THE BUTTON BELOW TO GO TO THE RUBY MAIN COURSE PAGE.
© License: All Rights Reserved
CONTACT HOLLA TECH – LEARN SUPPORT