# Is\_class from a String?

**URL:** <https://rubytalk.org/t/is-class-from-a-string/35209>\
**Category:** ruby-talk\
**Created:** [9 February 2007 00:25 UTC](https://rubytalk.org/t/is-class-from-a-string/35209 "2007-02-09T00:25:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![leandro\_nascimento\_c](https://avatars.discourse-cdn.com/v4/letter/l/13edae/32.png) [@leandro\_nascimento\_c](https://rubytalk.org/u/leandro_nascimento_c)\
**Post date:** [9 February 2007 00:25 UTC](https://rubytalk.org/t/is-class-from-a-string/35209/1 "2007-02-09T00:25:06Z")

</div>

I have a dynamic String entry and I want know if it represents some  
any existing Class name (in a given modules set loaded)?

How to do that.  
PS.: I'm using Rails and I need to know if there is some Model with a  
given name (or symbol).

---

<div class="post-metadata">

**Author:** ![Chris\_Carter](https://avatars.discourse-cdn.com/v4/letter/c/a8b319/32.png) [@Chris\_Carter](https://rubytalk.org/u/Chris_Carter)\
**Post date:** [9 February 2007 00:47 UTC](https://rubytalk.org/t/is-class-from-a-string/35209/2 "2007-02-09T00:47:42Z")

</div>

> I have a dynamic String entry and I want know if it represents some  
> any existing Class name (in a given modules set loaded)?
> 
> How to do that.  
> PS.: I'm using Rails and I need to know if there is some Model with a  
> given name (or symbol).

Hi Leandro:

> > class Model  
> > end

=\> nil

> > Module.const\_get("Model").is\_a? Class

=\> true

> **···**
>
> On 2/8/07, leandro nascimento camargo \<leandroico@gmail.com\> wrote:
> 
> --  
> Chris Carter  
> concentrationstudios.com  
> brynmawrcs.com

---

<div class="post-metadata">

**Author:** ![leandro\_nascimento\_c](https://avatars.discourse-cdn.com/v4/letter/l/13edae/32.png) [@leandro\_nascimento\_c](https://rubytalk.org/u/leandro_nascimento_c)\
**Post date:** [9 February 2007 01:10 UTC](https://rubytalk.org/t/is-class-from-a-string/35209/3 "2007-02-09T01:10:12Z")

</div>

Hi Chris...  
But If Model doesn't exist I will get an error. =/  
You see know the problem?  
But maybe I can use const\_defined before. =]  
I will test here.  
Thank you, anyway.

> **···**
>
> > Hi Leandro:  
> > \>\> class Model  
> > \>\> end  
> > =\> nil  
> > \>\> Module.const\_get("Model").is\_a? Class
> > 
> > =\> true
> > 
> > --  
> > Chris Carter  
> > concentrationstudios.com  
> > brynmawrcs.com

---

<div class="post-metadata">

**Author:** ![leandro\_nascimento\_c](https://avatars.discourse-cdn.com/v4/letter/l/13edae/32.png) [@leandro\_nascimento\_c](https://rubytalk.org/u/leandro_nascimento_c)\
**Post date:** [9 February 2007 01:30 UTC](https://rubytalk.org/t/is-class-from-a-string/35209/4 "2007-02-09T01:30:11Z")

</div>

Yay!  
Solved!  
I simply used an exception treatment like this:

is\_model =  
&nbsp;&nbsp;begin  
&nbsp;&nbsp;&nbsp;&nbsp;ActiveRecord.const\_get([c.name](http://c.name)[0...-3].camelize).is\_a? Class  
&nbsp;&nbsp;rescue StandardError =\> e  
&nbsp;&nbsp;&nbsp;&nbsp;false  
&nbsp;&nbsp;end
