In this section we will learn what the super keyword is and how to use it in Ruby.
Note: we’re assuming you’re already familiar with the Ruby method overriding.
What is super keyword in Ruby?
When overriding a method from a parent class, the problem you’ll face is that you can’t call the same method of the parent anymore! This is because your program always chooses the closest method, starting from the child class, and then if it couldn’t find the method, then it will check the parent methods for the target method. So when you’re overriding a method in a child class, there’s no reason for your program to check any parent class (because the method is already in the child class!)
But if you still want to call the same method of the parent class, Ruby has provided a keyword called `super` which tells your program that you want to call a method of the parent class instead of searching for the method in the child class!
Alright, let’s first see the syntax of the super keyword and then we will continue our discussion on how this keyword works in Ruby.
Ruby super method syntax:
super super (arguments) super()
If you call the `super` keyword without any argument (even without a pair of parentheses) within the body of an overridden method, in that case Ruby will call the same method in the parent class and uses all the arguments that was passed to the overridden method (if any) and passes them to the same method of the parent class.
But if you call the super keyword with just an empty pair of parentheses like `super()` then no argument will be passed to the same method of the parent class.
Of course, you still have the option of calling the super keyword and pass any necessary argument to it and the target method in the parent class will be invoked with those arguments.
Example: using the super method in Ruby
class Person def identity (first_name, last_name, id) @first_name = first_name @last_name = last_name @id = id end end class Employee <Person def identity (first_name, last_name, id) super end def print_identity puts @first_name, @last_name, @id end end emp = Employee.new emp.identity("John","Doe",500) emp.print_identity
Output:
John Doe 500
How does super method work in Ruby?
The `identity` method in this example is overridden by the Employee class. So by default, if we call this method, only the `identity` method of the Employee class will be called! So what we did here is to call the `super` keyword in the body of this method and because the super keyword is invoked without the use of any parentheses and arguments, it will automatically and behind the scene takes the arguments of the Employee’s identity method and passes them to the Person class `identity` method.
That’s how the three instance variables got initialized with the specified arguments.
Example: using super method with arguments in Ruby
Alright, now let’s refactor the example above to see there’s no difference on calling the super keyword with every argument of the enclosing method compared to just invoking the keyword without any argument at all.
class Person def identity (first_name, last_name, id) @first_name = first_name @last_name = last_name @id = id end end class Employee <Person def identity (first_name, last_name, id) super(first_name, last_name, id) end def print_identity puts @first_name, @last_name, @id end end emp = Employee.new emp.identity("John","Doe",500) emp.print_identity
Output:
John Doe 500