我想在ruby vs class和vs module中获得实现singleton模式的区别。我只是用类方法讨论singleton,没有实例。对于我来说,这是合乎逻辑的使用Ruby Singleton,模块vs类
module Foo
def self.foo= other
@@foo=other
end
def self.foo
@@foo
end
end
但很多时候我在别人的代码class Foo;....;end
看,我想知道为什么吗?如果没有实例,并没有子类Module
更方便。或者我可能会想念什么?
问题是单模式实现中模块和类之间的差异是什么?
实际上,有很多方法可以在Ruby中实现Singletons。我发现下面的文章颇有启发:https://www.practicingruby.com/articles/ruby-and-the-singleton-pattern-dont-get-along – user1934428