2010-08-30 5 views

Antwort

18

Dies bezieht sich explizit auf die MyClass im globalen Bereich. Wenn eine MyClass im globalen Bereich, aber auch eine MyClass innerhalb von SomeModule vorhanden ist, bezieht sich die Bezugnahme auf MyClass innerhalb von SomeModule innerhalb des Moduls auf MyClass, nicht auf die globale MyClass. Saying :: MyClass verweist explizit auf die MyClass im globalen Bereich.

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

"globaler Bereich" bedeutet nur, dass die Konstante in der Klasse Object definiert ist. So ist ::SomeModule eine Abkürzung für Object::SomeModule