有时我看到这样的代码:
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
之间有什么不同它只是编码风格,还是与众不同?
在上课/上课前,
::
做什么?●清除类/模块的命名空间(模块名)因为class Store < Application
有效,但这不起作用:class Store < ::Application
。请告诉我有什么不同…谢谢! 最佳答案
“::”是基(全局)作用域运算符。
因此,“::application”引用当前作用域中作为“application”引用应用程序的基础应用程序。
例如
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