范围操作符在Ruby中做什么,它的目的是什么?:: MyClass Ruby scope operator是做什么的?
9
A
回答
18
这明确引用了全局范围内的MyClass。如果全局范围内有MyClass,而且SomeModule内部还有MyClass,则引用SomeModule内部的MyClass将引用模块内部的MyClass,而不是全局MyClass。 Saying :: MyClass明确引用全局范围中的MyClass。
class MyClass
def self.something
puts "Global MyClass"
end
end
module SomeModule
class MyClass
def self.something
puts "SomeModule::MyClass"
end
end
print "From the module: "
MyClass.something
print "Explicitly using global scope: "
::MyClass.something
end
print "From the global scope: "
MyClass.something
print "Explicitly using module scope: "
SomeModule::MyClass.something
2
“全局范围”只是表示在Object
类中定义的常量。所以::SomeModule
是Object::SomeModule
相关问题
- 1. 做什么:scope“provided”是什么意思?
- 2. python operator = - 做什么?
- 3. C++中的operator()()是做什么的?
- 4. 为什么需要用std :: string :: operator +()显式调用Myclass :: operator string()?
- 5. 什么是C?:operator的Ruby等价物?
- 6. void Classname :: operator()(){....}是做什么用的?
- 7. C++ new operator scope
- 8. ruby_executable_hooks是做什么的?
- 9. 为什么要做$ scope。$ apply()?
- 10. 什么是Scope Creep?
- 11. 方法定义中的operator +做什么?
- 12. 使用myclass :: operator [](int i)
- 13. 这是什么C#构造在做什么,为什么? MyClass <TMyClass>:MyClass其中
- 14. 什么是“:: operator new”和“:: operator delete”?
- 15. AngularJS中的$ scope是什么?
- 16. operator()重载时,'const'做什么?
- 17. 这个operator()语法做了什么?
- 18. Ruby HTTP的read_status_line是做什么的?
- 19. 什么新MyClass的和新MyClass的()
- 20. C++:这个operator ^是什么?
- 21. Ruby中send()是做什么的?
- 22. Ruby on Rails'method'to_sym'是做什么的?
- 23. 为什么MyClass .__ class__返回的值不是MyClass().__ class__?
- 24. Ruby的Array#shift做什么?
- 25. ruby operator“=〜”
- 26. 什么“?”做?什么是__FILE__?
- 27. 在Ruby中什么是“等于大于”operator =>?
- 28. 什么是做%,而在Ruby中
- 29. :: operator new []()与:: operator new()不同的方式是什么?
- 30. 什么是is-operator的用法