There is an ugly (still working) hack using ObjectSpace. For example, something you should never use other than a game and possibly debugging. You just need the first (and only) instance, so:
ObjectSpace.each_object(self).first
To determine if it is a singleton class, you can use the weird property that ancestors will not include its receiver if it is a singleton class (or eigenclass or magic class):
ObjectSpace.each_object(self).first unless ancestors.include? self
If you care about edgecases, there are three objects whose classes are also their singleton classes.
[true, false, nil].each do |o| o.class.send(:define_method, :attached) { o } end
source share