Overriding Methods in Ruby

THU, 5 OCT 2006

If you are new to Ruby, you may bump into some confusion when overriding methods, especially when you still need to refer to the old or parent methods. Just to share some notes I have taken on overriding methods in Ruby.

You can override a method by either extending a class or by re-declaring an exisiting class with just the new methods.

Let’s dive into codes, Ruby codes speak a thousand words.

Overriding Parent Instance Method

This is the typical way in Object-Oriented programming, by extending a class.

class Parent
    def call_method
        'parent'
    end
end

class Child < Parent
    def call_method
        super + '-child'     #return: 'parent-child'
    end 
end

Overriding Parent Class Method

Class methods are a bit more confusing, because there are different ways to declare them. Well, welcome to ruby, there are a lot of magic here.

There are three ways to declare class methods:

class ClassName
    def ClassName.method_1
        ...
    end

    def self.method_2
        ...
    end

    class << self
        def method_3
            ...
        end

       def method_4
           ...
       end
    end
end

To override a class method:

class Parent
    def self.call_method
        'parent'
    end
end

class Child < Parent
     def self.call_method
         superclass.call_method + '-child'    #return: 'parent-child'
     end

end

Overriding Method of Existing Class

To override a method in an existing class, you just have to re-declare the class with the new methods. If you need to call the old method in your new method, you first have to create an alias of for the old method. You can use either alias or alias_method. This is something that I dont’ see in Java, pretty cool.

Note that you don’t need to redeclare Parent extension. And also note the difference in declaration between alias and alias_method; alias is a reserved keyword in Ruby, while alias_method is just a function call.

class Child
  alias old_call_method call_method

  def call_method
     old_call_method + '-new'   #return 'parent-child-new'
  end
end

class Child
  alias_method :old_call_method, :call_method

  def call_method
     old_call_method + '-new'   #return 'parent-child-new'
  end
end

Or you can also do it this way:

class Child
   alias_method :old_call_method, :call_method
   alias_method :call_method, :new_call_method

   def new_call_method
     old_call_method + '-new'   #return 'parent-child-new'
   end
end

# If you are using RailsOnEdge, this is the shortcut 
class Child
   alias_method_chain :call_method, :extra
  
   def call_method_with_extra
        call_method_without_extra + '-new'    #return 'parent-child-new'
   end
end

8 Comments . Comments Feed . Trackback URI
Fri, 6 Oct 06 11:42 pm . Cathal wrote:

Should it not read:

class Child < Parent

Tue, 10 Oct 06 08:34 am . Herryanto Siatono wrote:

Yeah…missed that out, thanks Cathal.

Mon, 4 Dec 06 07:14 am . Aslak Hellesøy wrote:

alias_method is not defined in Rails - it’s defined in Ruby’s Module class.

Mon, 4 Dec 06 09:42 am . Herryanto Siatono wrote:

Thanks for the correction Aslak, I’ve just updated the post.

Tue, 5 Jun 07 06:03 pm . George wrote:

Please give more detailed explanation on overriding methods. I am new to object oriented programming.

Wed, 6 Jun 07 03:16 pm . Volodymyr wrote:

Hello. Thank you a lot for your log. It was quite helpful. I was looking exactly for overriding methods in ruby. Good luck and keep logging.

Fri, 30 Nov 07 04:05 pm . adit wrote:

how to ovveride existing class method ?

Mon, 28 Jan 08 12:14 am . Ryan Shillington wrote:

I’m very confused - You have many examples up there where Child does not inherit from Parent. How could that possibly work?

Add Your Comment



(optional)