ruby super keyword

后端 未结 6 1268
天命终不由人
天命终不由人 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 21:16

    I added this method to find the owner of a method to my .irbrc, does anyone see a better way to do this, especially in handling singleton methods where the superclass of the singleton class is the singleton class of the superclass?

      class Object
        def find_method(method_string)
            if klasses = self.class.ancestors.select { |a| a if a.methods.include? method_string }
              puts "class method in #{klasses.join(',')}" unless klasses.empty?
            end
            if klasses = self.class.ancestors.select { |a| a if a.instance_methods.include? method_string }
              puts "instance method in #{klasses.join(',')}" unless klasses.empty?
            end
          rescue
            raise "owning class not found"
        end
      end
    

提交回复
热议问题