# Using a string to find a class

**URL:** <https://www.ruby-forum.com/t/using-a-string-to-find-a-class/95654>\
**Category:** Ruby\
**Created:** [June 8, 2006, 4:28am UTC](https://www.ruby-forum.com/t/using-a-string-to-find-a-class/95654 "2006-06-08T04:28:46Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [June 8, 2006, 4:28am UTC](https://www.ruby-forum.com/t/using-a-string-to-find-a-class/95654/1 "2006-06-08T04:28:46Z")

</div>

I’ve got a string, and I want to find out if there’s a class with that  
name. So something like

class\_exists? “Foo”

and then I can eval it if it does, I guess. Or is there a better way  
than eval in that case? At any rate, I just want to be able to take a  
string and find out if there’s a class with that name or not.

Thanks,  
Pat

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [June 8, 2006, 4:41am UTC](https://www.ruby-forum.com/t/using-a-string-to-find-a-class/95654/2 "2006-06-08T04:41:16Z")

</div>

Hi Pat

This ones come up a few times on the list

[http://www.ruby-forum.com/topic/66482#80672](http://www.ruby-forum.com/topic/66482#80672)

is one that I remember particulalry.

Although in some methods that I have seen in the rails source there has  
been

string\_name.camelize.constantize

This will return a class object specified by name.  
eg

def do\_somthing( class\_name )  
obj = class\_name.to\_s.camelize.constantize  
obj.new  
end

Hope this works for you

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [June 8, 2006, 5:02am UTC](https://www.ruby-forum.com/t/using-a-string-to-find-a-class/95654/3 "2006-06-08T05:02:27Z")

</div>

Pat M. wrote:

> I’ve got a string, and I want to find out if there’s a class with that  
> name. So something like
> 
> class\_exists? “Foo”
> 
> and then I can eval it if it does, I guess. Or is there a better way  
> than eval in that case? At any rate, I just want to be able to take a  
> string and find out if there’s a class with that name or not.

module.const\_get “Foo”

# or just

const\_get “Foo”

Cheers,  
Dave

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [June 8, 2006, 5:20am UTC](https://www.ruby-forum.com/t/using-a-string-to-find-a-class/95654/4 "2006-06-08T05:20:56Z")

</div>

Dave B. wrote:

> module.const\_get “Foo”
> 
> # or just
> 
> const\_get “Foo”

You’ll get an exception if Foo isn’t defined.

And not every constant refers to a class you can instantiate:

class Bar ; end  
kls = Object.const\_get “Bar”  
c = kls.new() # OK

Foo = 12  
kls = Object.const\_get “Foo”  
c = kls.new() # Not so OK

Quick version:

def class\_exists? kls  
begin  
kls = Object.const\_get kls  
kls.kind\_of? Class  
rescue Exception  
false  
end  
end

def new\_if\_class kls, default = nil  
class\_exists?( kls ) ? Object.const\_get( kls ).new : default  
end

b = new\_if\_class “Bar”  
p b

c = new\_if\_class “Foo”  
p c

–  
James B.

“Take eloquence and wring its neck.”

- Paul Verlaine

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [June 8, 2006, 3:43pm UTC](https://www.ruby-forum.com/t/using-a-string-to-find-a-class/95654/5 "2006-06-08T15:43:25Z")

</div>

On Thu, Jun 08, 2006 at 11:38:42AM +0900, Daniel N wrote:  
[…]  
} Although in some methods that I have seen in the rails source there  
has been  
}  
} string\_name.camelize.constantize  
}  
} This will return a class object specified by name.  
} eg  
}  
} def do\_somthing( class\_name )  
} obj = class\_name.to\_s.camelize.constantize  
} obj.new  
} end

Both #camelize and #constantize are Rails extensions.

} Hope this works for you  
–Greg

} On 6/8/06, Pat M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
} \>I’ve got a string, and I want to find out if there’s a class with  
that  
} \>name. So something like  
} \>  
} \>class\_exists? “Foo”  
} \>  
} \>and then I can eval it if it does, I guess. Or is there a better way  
} \>than eval in that case? At any rate, I just want to be able to take  
a  
} \>string and find out if there’s a class with that name or not.  
} \>  
} \>Thanks,  
} \>Pat  
} \>  
} \>  
}  
}

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [June 8, 2006, 3:43pm UTC](https://www.ruby-forum.com/t/using-a-string-to-find-a-class/95654/6 "2006-06-08T15:43:33Z")

</div>

On Thu, Jun 08, 2006 at 11:26:21AM +0900, Pat M. wrote:  
} I’ve got a string, and I want to find out if there’s a class with that  
} name. So something like  
}  
} class\_exists? “Foo”  
}  
} and then I can eval it if it does, I guess. Or is there a better way  
} than eval in that case? At any rate, I just want to be able to take a  
} string and find out if there’s a class with that name or not.

[http://redcorundum.blogspot.com/2006/05/kernelqualifiedconstget.html](http://redcorundum.blogspot.com/2006/05/kernelqualifiedconstget.html)

} Thanks,  
} Pat  
–Greg

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [June 8, 2006, 2:09pm UTC](https://www.ruby-forum.com/t/using-a-string-to-find-a-class/95654/7 "2006-06-08T14:09:10Z")

</div>

Use Facets’ kernel/constant

constant(name)

or string/to\_const

name.to\_const

http:://facets.rubyforge.org

T.
