5

我创建了一个包含常量NAME和方法的模块hello。如果一个类包含该模块,则两个定义都应该在不同的范围内可见。

module A
  NAME = 'Otto'
  def self.included(base)
    base.extend(ClassMethods)
  end

  def hello(name = 'world')
    self.class.hello(name)
  end

  module ClassMethods
    def hello(name = 'world')
      "Hello #{name}!"
    end
  end
end

class B
  include A

  def instance_scope
    p [__method__, hello(NAME)]
  end

  def self.class_scope
    p [__method__, hello(NAME)]
  end

  class << self
    def eigen_scope
      p [__method__, hello(NAME)]
    end
  end
end

B.new.instance_scope
B.class_scope
B.eigen_scope

#=> script.rb:34:in `eigen_scope': uninitialized constant Class::NAME (NameError)
    from script.rb:41

但是该常量在 eigenclass 的实例方法范围内不可见,class << self.

有没有办法使模块更健壮并在上述错误范围内提供常量?

4

1 回答 1

4

解决方案

class << self
  def eigen_scope
    p [__method__, hello(self::NAME)]
    #=> [:eigen_scope, "Hello Otto!"]
  end
end

为什么self::NAME有效?

  • A::NAME将是最简单的硬编码版本。
  • B::NAME也可以,因为B包括A
  • 里面eigen_scopeselfB,所以self::NAME也有效
  • self::NAME也可以在self.class_scope
  • self::NAME不起作用instance_scopeB实例不是类/模块。

为什么不起作用NAME

是一个很好的解释。

常量查找搜索定义在 Module.nesting,中的常量Module.nesting.first.ancestorsObject.ancestors如果Module.nesting.first是 nil 或模块

selfclass_scope在和中是一样的eigen_scope

Module.nesting虽然不同:

  • [B]为了class_scope
  • [#<Class:B>, B]为了eigen_scope

所以Module.nesting.first.ancestors是:

  • [B, A, Object, Kernel, BasicObject]为了class_scope
  • [#<Class:B>, A::ClassMethods, #<Class:Object>, #<Class:BasicObject>, Class, Module, Object, Kernel, BasicObject]为了eigen_scope

A没有被搜索,但是A::ClassMethods

所以你可以定义:

module A
  module ClassMethods
    NAME = 'Bob'
  end
end
于 2017-01-13T09:06:35.160 回答