Search code examples
rubyeigenclass

How to obtain the object corresponding to a given singleton (or eigen) class in Ruby?


Suppose the following code:

class A
end

a = A.new

As = class << a
    self
end

# or:
# As = a.singleton_class

Is there some way to get a from As?


Solution

  • I think it is simple:

    ObjectSpace.each_object(As).first
    # => #<A:0x000000029a7c50>