2011-02-16 112 views
0

當我嘗試將Singleton模塊包含在本身存在於模塊中的類中時,它不起作用。這裏有一個例子:ruby​​ NameError包含屬於另一個模塊的類中的Singleton模塊

require 'singleton' 

module SomeModule 
end 

class SomeModule::SomeClass 
    include Singleton 

    def initialize 
    @some_variable = 1 
    end 

    def output 
    puts @some_variable 
    end 
end 

SomeClass.instance.output 

,我得到的錯誤是:

未初始化的常量 對象:: SomeClass的(NameError)

我不知道如何告訴辛格爾頓模塊尋找SomeModule::SomeClass不是Object::SomeClass

回答

1

問題是你正在調用SomeClass類沒有th前置模塊名稱。添加模塊名稱以使其起作用:

SomeModule::SomeClass.instance.output 
+0

現在我明白了。猜猜這只是其中的一個。謝謝! – ZombieDev 2011-02-16 17:36:20

相關問題