Ruby 2 Overriding Methods

Method overriding, in object oriented programming, is a language feature that allows a subclass to provide a specific implementation of a method that is already provided by one of its superclasses. The implementation in the subclass overrides (replaces) the implementation in the superclass.
Here's an example p037xmtdovride.rb:
  1. class A  
  2.   def a  
  3.     puts 'In class A'  
  4.   end  
  5. end  
  6.   
  7. class B < A  
  8.   def a  
  9.     puts 'In class B'  
  10.   end  
  11. end  
  12.   
  13. b = B.new  
  14. b.a  
The method a in class B overrides the method a in class A.

Usage of super

The way super handles arguments is as follows:
  • When you invoke super with no arguments Ruby sends a message to the parent of the current object, asking it to invoke a method of the same name as the method invoking super. It automatically forwards the arguments that were passed to the method from which it's called.
  • Called with an empty argument list - super()-it sends no arguments to the higher-up method, even if arguments were passed to the current method.
  • Called with specific arguments - super(a, b, c) - it sends exactly those arguments.
An example (p038bicycle.rb) from Ruby for Rails book highlights this:
  1. class Bicycle  
  2.   attr_reader :gears:wheels:seats  
  3.   def initialize(gears = 1)  
  4.     @wheels = 2  
  5.     @seats = 1  
  6.     @gears = gears  
  7.   end  
  8. end  
  9.   
  10. class Tandem < Bicycle  
  11.   def initialize(gears)  
  12.     super  
  13.     @seats = 2  
  14.   end  
  15. end  
  16. t = Tandem.new(2)  
  17. puts t.gears  
  18. puts t.wheels  
  19. puts t.seats  
  20. b = Bicycle.new  
  21. puts b.gears  
  22. puts b.wheels  
  23. puts b.seats  
The output is:
  1. >ruby p038bicycle.rb  
  2. 2  
  3. 2  
  4. 2  
  5. 1  
  6. 2  
  7. 1  
  8. >Exit code: 0  
We shall be talking in depth about attr_reader later.

Redefining Methods

(Adapted from David Black's book, Ruby For Rails)
Nothing stops you from defining a method twice. Program p038or.rb
  1. class OR  
  2.   def mtd  
  3.     puts "First definition of method mtd"  
  4.   end  
  5.   def mtd  
  6.     puts "Second definition of method mtd"  
  7.   end  
  8. end  
  9. OR.new.mtd  
What happens when we call mtd on an instance of OR? Let's find out:
  1. OR.new.mtd  
The printed result is the Second definition of method mtd. The second definition has prevailed: We see the output from that definition, not from the first. Nothing stops you from defining a method twice, however the new version takes precedence.

Abstract class

In Ruby, we can define an abstract class that invokes certain undefined "abstract" methods, which are left for subclasses to define. For example:
  1. # This class is abstract; it doesn't define hello or name  
  2. # No special syntax is required: any class that invokes methods  
  3. # that are intended for a subclass to implement is abstract  
  4. class AbstractKlass  
  5.   def welcome  
  6.     puts "#{hello} #{name}"  
  7.   end  
  8. end  
  9.   
  10. # A concrete class  
  11. class ConcreteKlass < AbstractKlass  
  12.   def hello"Hello"end  
  13.   def name; "Ruby students"end  
  14. end  
  15.   
  16. ConcreteKlass.new.welcome # Displays "Hello Ruby students"