Using Superclass’s Method in Ruby
Ruby allows you to extend the behavior of the superclass instead of just overriding it and as a result replacing it. Ruby relies on keyword “super” to allow superclass methods to be available in the class we are calling from.
When we call super from within our own class, ruby will pass all the arguments to the same method of the superclass in exactly same way as they were received by subclass.
Example how superclass’s method can be used from within subclass is shown below.
class Car
def speed(miles, hours)
speed = miles/hours
return speed
end
class FastCar
def speed(miles, hours)
hours+=hours*2
super(miles, hours)
end
We can see from the example above that super was called with arguments after we performed additional calculations. This is one way of passing parameters. If we are to omit passing of the parameters and simply calling super from our FastCar.speed method, then super will take on the subclass parameters.
The “super” call can be made anywhere within method unlike Java implementation where you need to call super on the top. In fact, you can call super multiple times within a method.