# Verify if a class exists

**URL:** <https://rubytalk.org/t/verify-if-a-class-exists/38840>\
**Category:** ruby-talk\
**Created:** [4 July 2007 12:05 UTC](https://rubytalk.org/t/verify-if-a-class-exists/38840 "2007-07-04T12:05:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bernd](https://avatars.discourse-cdn.com/v4/letter/b/977dab/32.png) [@Bernd](https://rubytalk.org/u/Bernd)\
**Post date:** [4 July 2007 12:05 UTC](https://rubytalk.org/t/verify-if-a-class-exists/38840/1 "2007-07-04T12:05:47Z")

</div>

Hello everybody,

is there a way to check, if a class exists? I create classes dynamically  
and would like to know before, if a class with that name already exists.

Thanks for your help  
Bernd

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Guest20](https://avatars.discourse-cdn.com/v4/letter/g/ee7513/32.png) [@Guest20](https://rubytalk.org/u/Guest20)\
**Post date:** [4 July 2007 12:26 UTC](https://rubytalk.org/t/verify-if-a-class-exists/38840/2 "2007-07-04T12:26:59Z")

</div>

Try a const\_get. Like so:

def class\_exists?(name)  
&nbsp;&nbsp;begin  
&nbsp;&nbsp;&nbsp;&nbsp;true if Kernel.const\_get(name)  
&nbsp;&nbsp;rescue NameError  
&nbsp;&nbsp;&nbsp;&nbsp;false  
&nbsp;&nbsp;end  
end

> **···**
>
> On 7/4/07, Bernd \<burnt99@hotmail.com\> wrote:
> 
> > Hello everybody,
> > 
> > is there a way to check, if a class exists? I create classes dynamically  
> > and would like to know before, if a class with that name already exists.
> > 
> > Thanks for your help  
> > Bernd
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
> 
> --  
> - Simen

---

<div class="post-metadata">

**Author:** ![Bernd\_Burnt](https://avatars.discourse-cdn.com/v4/letter/b/aca169/32.png) [@Bernd\_Burnt](https://rubytalk.org/u/Bernd_Burnt)\
**Post date:** [4 July 2007 12:37 UTC](https://rubytalk.org/t/verify-if-a-class-exists/38840/3 "2007-07-04T12:37:17Z")

</div>

Simen Edvardsen wrote:

> > 
> 
> Try a const\_get. Like so:
> 
> def class\_exists?(name)  
> &nbsp;&nbsp;begin  
> &nbsp;&nbsp;&nbsp;&nbsp;true if Kernel.const\_get(name)  
> &nbsp;&nbsp;rescue NameError  
> &nbsp;&nbsp;&nbsp;&nbsp;false  
> &nbsp;&nbsp;end  
> end

Hi,  
first of all, thanks for your help, Simen. I think, I even found a  
better solution, I assign the name to the class with

Object.const\_set class\_name, klass

So I can determine, whether the class exists with

Object const\_defined? class\_name

You are using exception handling (rescue) for a conditional branch,  
which I try to avoid whenever possible, to keep my code well structured.

> **···**
>
> > On 7/4/07, Bernd \<burnt99@hotmail.com\> wrote:
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Guest20](https://avatars.discourse-cdn.com/v4/letter/g/ee7513/32.png) [@Guest20](https://rubytalk.org/u/Guest20)\
**Post date:** [4 July 2007 12:57 UTC](https://rubytalk.org/t/verify-if-a-class-exists/38840/4 "2007-07-04T12:57:31Z")

</div>

> Simen Edvardsen wrote:  
> \>\>  
> \>  
> \> Try a const\_get. Like so:  
> \>  
> \> def class\_exists?(name)  
> \> begin  
> \> true if Kernel.const\_get(name)  
> \> rescue NameError  
> \> false  
> \> end  
> \> end
> 
> Hi,  
> first of all, thanks for your help, Simen. I think, I even found a  
> better solution, I assign the name to the class with
> 
> Object.const\_set class\_name, klass
> 
> So I can determine, whether the class exists with
> 
> Object const\_defined? class\_name
> 
> You are using exception handling (rescue) for a conditional branch,  
> which I try to avoid whenever possible, to keep my code well structured.

There is a const\_defined? method? Ah, I see it's been some time since  
I programmed Ruby. Excuse my ignorant advice, it seems you find a  
better solution 🙂

> **···**
>
> On 7/4/07, Bernd Burnt \<djbearhand@gmx.de\> wrote:
> 
> > \> On 7/4/07, Bernd \<burnt99@hotmail.com\> wrote:
> 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
> 
> --  
> - Simen
