# What is the correct way to check for existence of virtual class?

**URL:** <https://www.ruby-forum.com/t/what-is-the-correct-way-to-check-for-existence-of-virtual-class/120394>\
**Category:** Ruby\
**Created:** [November 16, 2007, 2:48pm UTC](https://www.ruby-forum.com/t/what-is-the-correct-way-to-check-for-existence-of-virtual-class/120394 "2007-11-16T14:48:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Voroztsov\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a8b319/32.png) [@Voroztsov\_A](https://www.ruby-forum.com/u/Voroztsov_A)\
**Post date:** [November 16, 2007, 2:48pm UTC](https://www.ruby-forum.com/t/what-is-the-correct-way-to-check-for-existence-of-virtual-class/120394/1 "2007-11-16T14:48:21Z")

</div>

One can write:

class Object  
def singleton\_class  
class \<\<self; self; end rescue nil  
end  
end

puts “aaa”.singleton\_class  
puts 8.singleton\_class

Is it the assumed way to do the job?

Is there any special method in ruby 1.9?

---

<div class="post-metadata">

**Author:** ![Voroztsov\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a8b319/32.png) [@Voroztsov\_A](https://www.ruby-forum.com/u/Voroztsov_A)\
**Post date:** [November 16, 2007, 5:31pm UTC](https://www.ruby-forum.com/t/what-is-the-correct-way-to-check-for-existence-of-virtual-class/120394/2 "2007-11-16T17:31:24Z")

</div>

On Nov 16, 2007 8:47 AM, Voroztsov A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Is it the assumed way to do the job?

There’s a bit of a Heisenberg problem here, in that the act of  
measuring alters the result.

Singleton instance classes aren’t created until they are needed, but  
one of the things which creates them is class \<\<x;end so you’re really  
not checking whether or not the singleton class already exists this  
way.

Another way would be

!var.singleton\_methods.empty?

BTW, your 8.singleton\_class will fail, Fixnums can’t have singleton  
classes.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![Voroztsov\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a8b319/32.png) [@Voroztsov\_A](https://www.ruby-forum.com/u/Voroztsov_A)\
**Post date:** [November 16, 2007, 6:57pm UTC](https://www.ruby-forum.com/t/what-is-the-correct-way-to-check-for-existence-of-virtual-class/120394/3 "2007-11-16T18:57:36Z")

</div>

2007/11/16, Rick DeNatale [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > puts 8.singleton\_class
> 
> [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

Thanks!

I see.

In fact I want to create singleton class if it is possible.

I guess checking if singleton class already exists should be tricky  
coding or impossible.  
Let’s look at the code:

class Object  
def singleton\_class  
class \<\<self; self; end rescue nil  
end  
end

[“abc”, 8].each do |a|  
puts “\nObject: #{a}”

```
puts !a.singleton_methods.empty?

puts a.singleton_class

puts !a.singleton_methods.empty?

```

end

Output is

Object: abc  
false  
#\<Class:#String:0x2b28c50\>  
false

Object: 8  
false  
nil  
false

So it looks like expression

!a.singleton\_methods.empty?

can miss existence of singleton class.

But in fact these two problems have pragmatic sense:

1. Get singleton class if it is possible
2. Check if objects has singleton methods

While this one – not :  
3) Check if singleton class exists  
(now I understand that there is no use case for it)

Artem

---

<div class="post-metadata">

**Author:** ![Voroztsov\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a8b319/32.png) [@Voroztsov\_A](https://www.ruby-forum.com/u/Voroztsov_A)\
**Post date:** [November 16, 2007, 8:14pm UTC](https://www.ruby-forum.com/t/what-is-the-correct-way-to-check-for-existence-of-virtual-class/120394/4 "2007-11-16T20:14:34Z")

</div>

On Nov 16, 2007 5:56 PM, Voroztsov A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > > 
> > 
> > not checking whether or not the singleton class already exists this
> 
> [“abc”, 8].each do |a|  
> Output is
> 
> While this one – not :  
> 3) Check if singleton class exists  
> (now I understand that there is no use case for it)
> 
> Artem

FWIW, these are my experiments with trying to find a way to detect if  
I’m currently executing within a singleton scope:

def in\_singleton?  
if self.respond\_to?(:superclass)  
if superclass && superclass.superclass  
# the mystery of that which is its own parent \<#Class:Class\> (&  
grandparent, ad infinitum)  
superclass.superclass.superclass == superclass.superclass  
else  
false  
end  
else  
false  
end  
end  
public :in\_singleton?

class O  
in\_singleton? # =\> false  
end  
class O  
self.in\_singleton? # =\> false  
end  
class O  
class \<\< self  
in\_singleton? # =\> true  
end  
end

class P  
end

o = P.new  
o.in\_singleton? # =\> false  
class \<\< o  
in\_singleton? # =\> true  
end  
o.in\_singleton? # =\> false  
nil.in\_singleton? # =\> false  
NilClass.in\_singleton? # =\> false  
Object.in\_singleton? # =\> false  
Class.in\_singleton? # =\> false  
class Class  
in\_singleton? # =\> false  
class \<\< self  
in\_singleton? # =\> true  
end  
end  
class Qux  
in\_singleton? # =\> false  
class \<\< self  
in\_singleton? # =\> true  
end  
end

# but…

class Object  
in\_singleton? # =\> false  
class \<\< self  
in\_singleton? # =\> false  
end  
end

Regards,  
Sean
