我需要具有单例行为的类。
使用 Singleton 模块有什么区别...
require 'singleton'
class X
include Singleton
def set_x(x)
@x = x
end
def test
puts @x
end
end
X::instance.set_x('hello')
X::instance.test
...并使用类方法和类实例变量?
class X
def self.set_x(x)
@x = x
end
def self.test
puts @x
end
end
X::set_x('hello')
X::test