Calling ObjectSpace.each_object(Class)
does not seem to return any eigenclasses. For example, when investigating Ruby metaclasses: why three when defined singleton methods?, I found that while ObjectSpace.count_objects[:T_CLASS]
was getting incremented by 3 in the case of defining a new class with a class method, ObjectSpace.each_object(Class).count
was only being incremented by one.
Is there any way to enumerate the eigenclasses active in the system?
Looking at MRI C code, the function
ObjectSpace.each_object
tests if the object is an internal object and if it is true the object is removed from the iterator.The test is made by the following function, which consider the classes internally flagged as singleton as an internal object:
The Ruby EigenClasses are flagged as Singleton, so it will be not returned.
It is important to mention that, the singleton flag used internally by the MRI is different from the Singleton module from standard library.
If you create a class and include the Singleton module on it, it will be returned by the
ObjectSpace.each_object
method.