Issue #11360 has been updated by Benoit Daloze.


Koichi Sasada wrote:
> From Ruby 1.6, singleton classes are not appeared.
> Is it intentional?

I guess the reason is exposing the last layer of singleton classes would expose "VM-internal singleton classes/metaclasses".
The documentation of `rb_singleton_class` explains it a bit.
But basically the VM needs the metaclass of any class exposed to the user, and exposing them would require another layer, which if exposed as well would need another, etc.
Singleton classes which already have a metaclass could be iterated though.

----------------------------------------
Bug #11360: Singleton class doesn't appear by ObjectSpace.each_object
https://bugs.ruby-lang.org/issues/11360#change-53443

* Author: Koichi Sasada
* Status: Open
* Priority: Normal
* Assignee: Koichi Sasada
* ruby -v: 2.3dev
* Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN
----------------------------------------
Singleton class doesn't appear by ObjectSpace.each_object.

The following is reproducible code.

```ruby
class C
  class << self
    p [self, self.class]
    $c = self
  end
end

ObjectSpace.each_object(Class){|o|
  exit if $c == o
}
raise "#{$c} is not found!"
```

This is because internal_object_p in gc.c skips singleton classes.

```C
static int
internal_object_p(VALUE obj)
{
    RVALUE *p = (RVALUE *)obj;

    if (p->as.basic.flags) {
	switch (BUILTIN_TYPE(p)) {
	  case T_NONE:
	  case T_IMEMO:
	  case T_ICLASS:
	  case T_NODE:
	  case T_ZOMBIE:
	    break;
	  case T_CLASS:
	    if (FL_TEST(p, FL_SINGLETON))
	      break;
	  default:
	    if (!p->as.basic.klass) break;
	    return 0;
	}
    }
    return 1;
}
```




-- 
https://bugs.ruby-lang.org/