Calling method in parent class from subclass methods in Ruby

前端 未结 5 454
粉色の甜心
粉色の甜心 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:28

    The super keyword calls a method of the same name in the super class:

    class Foo
      def foo
        "#{self.class}#foo"
      end
    end
    
    class Bar < Foo
      def foo
        "Super says: #{super}"
      end
    end
    
    Foo.new.foo # => "Foo#foo"
    Bar.new.foo # => "Super says: Bar#foo"
    

提交回复
热议问题