範圍操作符在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. ruby_executable_hooks是做什麼的?
- 8. 什麼是Scope Creep?
- 9. 使用myclass :: operator [](int i)
- 10. 方法定義中的operator +做什麼?
- 11. 這個operator()語法做了什麼?
- 12. operator()重載時,'const'做什麼?
- 13. C++:這個operator ^是什麼?
- 14. Ruby on Rails'method'to_sym'是做什麼的?
- 15. Ruby中send()是做什麼的?
- 16. 爲什麼MyClass .__ class__返回的值不是MyClass().__ class__?
- 17. ruby operator「=〜」
- 18. Ruby的Array#shift做什麼?
- 19. 什麼「?」做?什麼是__FILE__?
- 20. class myClass <String>是什麼意思?
- 21. 這是什麼:\ My \ Namespace \ MyClass :: class?
- 22. Ruby代碼在做什麼?
- 23. 在Ruby中做什麼==?
- 24. 「|| =」在Ruby 1.9.2中做什麼?
- 25. set_if_nil.call在ruby中做什麼?
- 26. scala.Predef中$ scope的用途是什麼?
- 27. scope = ScopeType.PAGE的新註釋是什麼?
- 28. jQuery中的$ scope是指什麼?
- 29. AS3 - 什麼是MyClass的(實例)和(實例作爲MyClass的)之間的差別
- 30. AngularJS指令 - 「scope:{}」是什麼意思?