How do I call a super class method How do I call a super class method ruby ruby

How do I call a super class method


In Ruby 2.2, you can use Method#super_method now

For example:

class B < A  def foo    super + " world"  end  def bar    method(:foo).super_method.call  endend

Ref: https://bugs.ruby-lang.org/issues/9781#change-48164 and https://www.ruby-forum.com/topic/5356938


You can do:

 def bar   self.class.superclass.instance_method(:foo).bind(self).call end


In this particular case you can just alias :bar :foo before def foo in class B to rename the old foo to bar, but of course you can alias to any name you like and call it from that. This question has some alternative ways to do it further down the inheritance tree.