Calling method in parent class from subclass methods in Ruby

前端 未结 5 453
粉色の甜心
粉色の甜心 2020-12-24 05:20

I\'ve used super to initialize parent class but I cannot see any way of calling parent class from subclass methods.

I know PHP and other languages do ha

5条回答
  •  礼貌的吻别
    2020-12-24 05:35

    The super keyword in Ruby actually calls a method of the same name in the parent class. (source)

    class Foo
      def foo
        # Do something
      end
    end
    
    class Bar < Foo
      def foo
        super # Calls foo() method in parent class
      end
    end
    

提交回复
热议问题