[英]How does '::' at the beginning of the class affect scope (ruby/rails)?
我在一个大型Rails项目中工作,其中有一些文件:
app / services / my_service / my_action / my_model_2.rb :
module <my_service>
module <my_action>
class <my_model_2>
. . .
if some_var = <my_model>::MY_CONST
if some_Var = ::<my_model>::MY_CONST # <-- what is the difference here?
app / services / my_service / my_action / my_model.rb :
module <my_service>
module <my_action>
class <my_model>
app / services / my_service / my_model.rb :
module <my_service>
class <my_model>
app / models / my_model.rb :
class <my_model> < ActiveRecord::Base
. . .
MY_CONST = "my constant"
my_model_2.rb有什么区别?
我如何正确确定范围以从my_model_2.rb访问每个模型?
请注意,app / models类名与app / services类名相同。
前导::
强制Ruby解释器从顶级名称空间开始解析类名。 这意味着,如果您有一个Foo::Bar::Baz
类和两个都定义的Boo
类/模块,
在Foo::Bar::Baz
引用Boo
实际上将最终到达Foo::Boo
模块/类,而如果使用::Boo
,它将与Boo
匹配,因为名称的解析将从主命名空间开始。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.