有时我会看到这样的代码:
module Elite
module App
def app
'run app'
end
end
module Base
def base
'base module'
end
end
class Application
include Elite::Base #Include variant A
include ::Elite::App #Include variant B
def initialize(str=nil)
puts "Initialized with #{str}"
puts "Is respond to base?: #{base if self.respond_to?(:base)}"
puts "Is respond to app?: #{app if self.respond_to?(:app)}"
end
end
class Store < ::Elite::Application
def initialize(str=nil)
super #Goes to Application init
end
end
end
elite = Elite::Store.new(:hello)
但我不明白class Store < ::Elite::Application
和class Store < Elite::Application
或include Elite::Base
和include ::Elite::App
之间的区别
它只是编码风格,还是它的不同之处?
课程/模块之前::
做了什么? ::类/模块的clean名称空间(模块名称)?由于class Store < Application
有效,但不是:class Store < ::Application
。请告诉我有什么区别......谢谢!
答案 0 :(得分:3)
&#39; ::&#39;是基础(全局)范围运算符。
所以&#39; ::应用程序&#39;引用基础应用程序作为&#39;应用程序&#39;引用当前范围内的应用程序。
例如
class Application # Class 1
end
class Smile
class Application # Class 2
end
::Application # references class 1
Application # references class 2 (The application in my current scope)
end