# Best way to get a class object from its name?

**URL:** <https://www.ruby-forum.com/t/best-way-to-get-a-class-object-from-its-name/86519>\
**Category:** Ruby\
**Created:** [February 22, 2007, 10:21pm UTC](https://www.ruby-forum.com/t/best-way-to-get-a-class-object-from-its-name/86519 "2007-02-22T22:21:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [February 22, 2007, 10:21pm UTC](https://www.ruby-forum.com/t/best-way-to-get-a-class-object-from-its-name/86519/1 "2007-02-22T22:21:34Z")

</div>

All,

In my Rails app., I’m doing something like this:

eval("#{requested\_controller\_class}.respond\_to?(‘session\_data\_not\_needed\_by?’)")

where requested\_controller\_class is the name of an existing (controller)  
class.

Is the above code the best of way of invoking respond\_to? on the class  
in question, or is there some better way to get the actual class object  
based solely on it’s name?

Thanks,  
Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [February 22, 2007, 10:35pm UTC](https://www.ruby-forum.com/t/best-way-to-get-a-class-object-from-its-name/86519/2 "2007-02-22T22:35:55Z")

</div>

Hi~

On Feb 22, 2007, at 1:21 PM, Wes G. wrote:

> Is the above code the best of way of invoking respond\_to? on the class  
> in question, or is there some better way to get the actual class  
> object  
> based solely on it’s name?
> 
> Thanks,  
> Wes

Since you are in a Rails app the better way of doing that would be  
like this:

requested\_controller\_class.constantize.respond\_to? :session\_data\_not\_nee  
ded\_by?

Cheers-  
– Ezra Z.  
– Lead Rails Evangelist  
– removed\_email\_address@domain.invalid  
– Engine Y., Serious Rails Hosting  
– (866) 518-YARD (9273)

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [February 22, 2007, 10:40pm UTC](https://www.ruby-forum.com/t/best-way-to-get-a-class-object-from-its-name/86519/3 "2007-02-22T22:40:55Z")

</div>

On Feb 22, 2:21 pm, Wes G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> in question, or is there some better way to get the actual class object  
> based solely on it’s name?
> 
> Thanks,  
> Wes
> 
> –  
> Posted viahttp://www.ruby-forum.com/.

Getting class objects was one of the problems in Ruby Q. #113. See:  
[One-Liners (#113) - Ruby - Ruby-Forum](http://www.ruby-forum.com/topic/97203) or  
[http://groups.google.com/group/comp.lang.ruby/browse\_thread/thread/90223b4082d864fa/](http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/90223b4082d864fa/)

There are a couple of solutions given. Assuming your class name is in  
the variable named ‘quiz’ this (from James II) does it:  
quiz.split(“::”).inject(Object) { |par, const| par.const\_get(const) }

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [February 22, 2007, 10:41pm UTC](https://www.ruby-forum.com/t/best-way-to-get-a-class-object-from-its-name/86519/4 "2007-02-22T22:41:56Z")

</div>

On Fri, Feb 23, 2007, Wes G. wrote:

> eval("#{requested\_controller\_class}.respond\_to?(‘session\_data\_not\_needed\_by?’)")
> 
> where requested\_controller\_class is the name of an existing (controller)  
> class.
> 
> Is the above code the best of way of invoking respond\_to? on the class  
> in question, or is there some better way to get the actual class object  
> based solely on it’s name?

> > str = “ActiveRecord::Base”  
> > =\> “ActiveRecord::Base”
> 
> > str.constantize.respond\_to? :new  
> > =\> true

String#constantize is provided by Rails someplace. In non-Rails R.  
code, you could do this:

> > Module.const\_get( ‘ActiveRecord’ ).const\_get( ‘Base’ ).respond\_to?  
> > :find  
> > =\> true

Of course, if it’s a single-level, you only need one of those.

Cheers,  
Ben
