What is the inverse of Module#singleton_class? I.e., given a singleton class, how can I get the module it is the singleton of?
Module#singleton_class
You can use ObjectSpace#each_object for that:
module M; end sc = M.singleton_class ObjectSpace.each_object(Module).find { |m| m.singleton_class == sc } #=> M
@ndn pointed out that:
ObjectSpace.each_object(sc).to_a #=> [M]
so it's just:
ObjectSpace.each_object(sc).first #=> M
2.1m questions
2.1m answers
60 comments
57.0k users