Why 'self' method of module cannot become a singleton method of class?

前端 未结 2 940
伪装坚强ぢ
伪装坚强ぢ 2020-12-23 12:57
module Test
  def self.model_method
    puts \"this is a module method\"
  end
end

class A
  include Test
end

A.model_method

this will be error w

2条回答
  •  感情败类
    2020-12-23 13:24

    Including a module is analogous to copying its instance methods over.

    In your example, there are no instance methods to copy to A. model_method is actually an instance method of Test's singleton class.


    Given:

    module A
      def method
      end
    end
    

    This:

    module B
      include A
    end
    

    Is analogous to this:

    module B
      def method
      end
    end
    

    When you think of it this way, this makes perfect sense:

    module B
      class << self
        include A
      end
    end
    
    B.method
    

    Here, the methods are being copied to the B module's singleton class, which makes them the "class methods" of B.

    Note that this is exactly the same thing as:

    module B
      extend A
    end
    

    In reality, the methods are not being copied; there is no duplication. The module is simply included in the method lookup list.

提交回复
热议问题