When I try to include the Singleton module in a class that itself exists in a module it does not work. Here's an example:
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
and the error I get is:
uninitialized constant Object::SomeClass (NameError)
I'm not sure how to tell the Singleton module to look for SomeModule::SomeClass
not开发者_StackOverflow中文版 Object::SomeClass
The problem is you are calling the SomeClass class without the prepended module name. Add the module name to get it to work:
SomeModule::SomeClass.instance.output
精彩评论