反向`Module#singleton_class`

Module#singleton_class的反转是什么? 即,给定一个单例类,我怎样才能使模块成为单例?

您可以使用ObjectSpace#each_object :

 module M; end sc = M.singleton_class ObjectSpace.each_object(Module).find { |m| m.singleton_class == sc } #=> M 

编辑:@ndn指出:

 ObjectSpace.each_object(sc).to_a #=> [M] 

所以它只是:

 ObjectSpace.each_object(sc).first #=> M