Tag: 单身

调用Ruby Singleton的方法而不引用’instance’

我想调用Singleton对象的方法而不引用它的实例 SingletonKlass.my_method 代替 SingletonKlass.instance.my_method 我想出了这个解决方案(在课堂上使用method_missing): require ‘singleton’ class SingletonKlass include Singleton def self.method_missing(method, *args, &block) self.instance.send(method, *args) end def my_method puts “hi there!!” end end 这有什么缺点吗? 还有更好的解决方案吗? 你的任何推荐? 谢谢。 更新: 我的目标是让一个模块与单例类混合: module NoInstanceSingleton def method_missing(method, *args) self.instance.send(method, *args) end end 结束然后在课堂上使用它: class SingletonKlass include Singleton extend NoInstanceSingleton def method1; end def method2; end … def […]