Last Updated: February 25, 2016
·
2.813K
· lukasalexandre

Method override with super()

One of the things that I love in ruby is the ability to easily replace a method behavior with something of your own.

class MyClass
  def old_method(arg)
    puts "default behavior -> #{arg}"
  end
end

class MyNewClass < MyClass
  def old_method(arg)
    puts "new behavior #{arg}"
  end
end

MyClass.new().old_method("example1") # default behavior -> example1
MyNewClass.new().old_method("example1") # new behavior -> example1

But as we all know, we should be carefull because this method might be doing something that you did not predicted, leading you into a trap. One of the things that we should always consider is to invoke the super() method inside our implementation, which refers back to the original method behavior.

class MyNewClass < MyClass
  def old_method(arg)
    puts "new behavior #{arg}"
    super(arg)
  end
end

MyNewClass.new().old_method("example2")
# new behavior -> example2
# default behavior -> example2

It is common to not know if a method has already been defined. In that case, calling the super() would result in a NoMethodError: super: no superclass method error. To solve that uncertainty you can use the defined? operator to check the existance of a super method.

class MyNewClass < MyClass
  def method_without_super(arg)
    puts "no super -> #{arg}"
    super(arg) if defined? super
  end
end

MyNewClass.new().method_without_super("example3")
# no super -> example3

One thing that most people do not know is that Ruby can pass automagically the method params to its super.

class MyNewClass < MyClass
  def old_method(arg)
    puts "new behavior #{arg}"
    super
  end
end

MyNewClass.new().old_method("example4")
# new behavior -> example4
# default behavior -> example4

By calling the super without any params Ruby will get all previous params and pass them all along the super() call.

One quick gotcha about this. If you are using the define_method() signature this params shortcut won't work.

1 Response
Add your response

nice. there's a typo in "you did not predicted"

over 1 year ago ·