This is the inverse of the question "Given an instance of a Ruby object, how do I get its metaclass?"
You can see a representation of the object to which a metaclass or singleton class is attached in the default to_s
output:
s = "hello"
s_meta = class << s; self; end
s_meta.to_s # => "#<Class:#<String:0x15004dd>>"
class C; end
c_meta = class << C; self; end
c_meta.to_s # => "#<Class:C>"
Is it possible to implement a method Class.attached
that returns this object (or nil if the receiver is a regular class)?
s_meta.attached # => s
c_meta.attached # => C
C.attached # => nil