Calling method in parent class from subclass methods in Ruby

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

    Others have said it already well. Just one additional note of caution:

    The syntax super.foo to call method foo in the super class is not supported. Rather it will call the super-method and on the returned result, try to call foo.

      class A
        def a
          "A::a"
        end
      end
    
      class B < A
        def a
          "B::a is calling #{super.a}" # -> undefined method `a` for StringClass 
        end
      end
    

提交回复
热议问题