# Is\_class from a String?

**URL:** <https://www.ruby-forum.com/t/is-class-from-a-string/84989>\
**Category:** Ruby\
**Created:** [February 9, 2007, 1:25am UTC](https://www.ruby-forum.com/t/is-class-from-a-string/84989 "2007-02-09T01:25:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![leandro\_nascimento\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f0a364/32.png) [@leandro\_nascimento\_S](https://www.ruby-forum.com/u/leandro_nascimento_S)\
**Post date:** [February 9, 2007, 1:25am UTC](https://www.ruby-forum.com/t/is-class-from-a-string/84989/1 "2007-02-09T01:25:35Z")

</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:** ![leandro\_nascimento\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f0a364/32.png) [@leandro\_nascimento\_S](https://www.ruby-forum.com/u/leandro_nascimento_S)\
**Post date:** [February 9, 2007, 1:48am UTC](https://www.ruby-forum.com/t/is-class-from-a-string/84989/2 "2007-02-09T01:48:36Z")

</div>

On 2/8/07, leandro nascimento camargo [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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

---

<div class="post-metadata">

**Author:** ![leandro\_nascimento\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f0a364/32.png) [@leandro\_nascimento\_S](https://www.ruby-forum.com/u/leandro_nascimento_S)\
**Post date:** [February 9, 2007, 2:10am UTC](https://www.ruby-forum.com/t/is-class-from-a-string/84989/3 "2007-02-09T02:10:37Z")

</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.

---

<div class="post-metadata">

**Author:** ![leandro\_nascimento\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f0a364/32.png) [@leandro\_nascimento\_S](https://www.ruby-forum.com/u/leandro_nascimento_S)\
**Post date:** [February 9, 2007, 2:30am UTC](https://www.ruby-forum.com/t/is-class-from-a-string/84989/4 "2007-02-09T02:30:39Z")

</div>

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

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