2010-09-08 80 views
4

在ruby中,:: namespaces模块和类。 但我经常看到::在类名的开头如下所示:ruby​​中的类名之前的符号/运算符是什么?

#snippet of gollum gem 
def page_class 
    @page_class || 
    if superclass.respond_to?(:page_class) 
     superclass.page_class 
    else 
     ::Gollum::Page 
    end 
end 

是什么::表示如果在开始?

+0

的可能重复[这是什么className类<:: OtherClassName在Ruby中做什么?(http://StackOverflow.Com/questions/3302062/)和[什么: :MyClass Ruby范围运算符呢?](http://StackOverflow.Com/questions/3597096/)和其他人。 – 2010-09-08 11:11:40

+0

http://stackoverflow.com/questions/3597096/what-does-myclass-ruby-scope-operator-do – agentbanks217 2010-09-08 10:38:35

回答

14

这是解决对全球范围内,而不是本地的。

class A 
    def self.global? 
    true 
    end 
end 

module B 

    class A 
    def self.global? 
    false 
    end 
    end 

    def self.a 
    puts A.global? 
    puts ::A.global? 

    end 
end 

B::a 

打印

false 
true 
相关问题