相关疑难解决方法(0)

为什么模块的单例方法在混合的下游特征类中不可见?

我理解常规方法查找路径即class, superclass/module, all the way up to BasicObject.我认为对于单链版本的链也是如此,但是当您在元链中混合模块时似乎并非如此.我很感激,如果有人能解释为什么在下面的示例中,当我将此模块包含在Vehicle的本征类中时,调用Automobile模块的banner方法而不是单例版本.

module Automobile
  def banner
    "I am a regular method of Automobile"
  end

  class << self
    def banner
      "I am a singleton method of Automobile"
    end
  end
end

class Vehicle 
  def banner
    "I am an instance method of Vehicle"
  end

  class << self
    include Automobile
    def banner
      puts "I am a singleton method of Vehicle"
      super
    end
  end
end

class Car < Vehicle
  def banner
    "I am …
Run Code Online (Sandbox Code Playgroud)

ruby inheritance mixins eigenclass

7
推荐指数
1
解决办法
1012
查看次数

标签 统计

eigenclass ×1

inheritance ×1

mixins ×1

ruby ×1