ruby super keyword

后端 未结 6 1265
天命终不由人
天命终不由人 2020-12-12 20:45

From what I understand, super keyword invokes a method with the same name as the current method in the superclass of the current class. Below in the autol

6条回答
  •  半阙折子戏
    2020-12-12 20:56

    Use Pry

    Insert a binding.pry call right before you use super, and then invoke show-source -s (-s means superclass) to show the superclass method and find out where it's defined:

    class A
      def hello
        puts "hi"
      end
    end
    
    class B < A
      def hello
        binding.pry
        super
      end
    end
    
    b = B.new
    b.hello
    
    From: (pry) @ line 7 B#hello:
    
         7: def hello
     =>  8:   binding.pry
         9:   super
        10: end
    
    [1] (pry) #: 0> show-source -s
    
    From: (pry) @ line 2:
    Number of lines: 3
    Owner: A   # <--see owner here (i.e superclass)
    Visibility: public
    
    def hello
      puts "hi"
    end
    [2] (pry) #: 0>    
    

提交回复
热议问题