# Class from string

**URL:** <https://rubytalk.org/t/class-from-string/27868>\
**Category:** ruby-talk\
**Created:** [30 May 2006 23:52 UTC](https://rubytalk.org/t/class-from-string/27868 "2006-05-30T23:52:46Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dan\_Kirkwood](https://avatars.discourse-cdn.com/v4/letter/d/cc9497/32.png) [@Dan\_Kirkwood](https://rubytalk.org/u/Dan_Kirkwood)\
**Post date:** [30 May 2006 23:52 UTC](https://rubytalk.org/t/class-from-string/27868/1 "2006-05-30T23:52:46Z")

</div>

Is there a better way to get to a class when you have the class name in  
a string?  
(This is rails-related, btw).. For instance, if I have a string clname  
= 'Train', and I want to access a class method Train#label. I can do  
this:

clname = 'Train'  
label = eval "#{clname}.label"

Is there a better way?

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

---

<div class="post-metadata">

**Author:** ![Victor\_Zverok\_Shepel](https://avatars.discourse-cdn.com/v4/letter/v/a88e57/32.png) [@Victor\_Zverok\_Shepel](https://rubytalk.org/u/Victor_Zverok_Shepel)\
**Post date:** [31 May 2006 00:04 UTC](https://rubytalk.org/t/class-from-string/27868/2 "2006-05-31T00:04:18Z")

</div>

Of Dan Kirkwood

> Is there a better way to get to a class when you have the class name in  
> a string?  
> (This is rails-related, btw).. For instance, if I have a string clname  
> = 'Train', and I want to access a class method Train#label. I can do  
> this:
> 
> clname = 'Train'  
> label = eval "#{clname}.label"
> 
> Is there a better way?

Aha.

clname = 'Train'  
label = Object.const\_get(clname).new  
p label.class #=\> Train

V.

> **···**
>
> From: list-bounce@example.com [mailto:list-bounce@example.com] On Behalf  
> Sent: Wednesday, May 31, 2006 2:53 AM

---

<div class="post-metadata">

**Author:** ![Ryan\_Leavengood](https://avatars.discourse-cdn.com/v4/letter/r/a587f6/32.png) [@Ryan\_Leavengood](https://rubytalk.org/u/Ryan_Leavengood)\
**Post date:** [31 May 2006 00:54 UTC](https://rubytalk.org/t/class-from-string/27868/3 "2006-05-31T00:54:07Z")

</div>

This really gets asked a whole lot, especially from Ruby Forum. I  
really, really, really wish there was a prominent FAQ on Ruby Forum  
with this as like question number 1. This would save people like Dan  
from having to ask the question and would save the rest of us from  
having to answer it for the 50th time.

Ryan

P.S. No offense Dan, this isn't your fault.

> **···**
>
> On 5/30/06, Dan Kirkwood \<dangogh@gmail.com\> wrote:
> 
> > Is there a better way to get to a class when you have the class name in  
> > a string?

---

<div class="post-metadata">

**Author:** ![Ezra\_Zygmuntowicz](https://avatars.discourse-cdn.com/v4/letter/e/958977/32.png) [@Ezra\_Zygmuntowicz](https://rubytalk.org/u/Ezra_Zygmuntowicz)\
**Post date:** [31 May 2006 01:41 UTC](https://rubytalk.org/t/class-from-string/27868/4 "2006-05-31T01:41:39Z")

</div>

In rails you can use #constantize

clname = 'Train'  
clname.constantize.label

But you really should try to search before posting. This was asked a bunch of times in the last few weeks.

Cheers-  
-Ezra

> **···**
>
> On May 30, 2006, at 4:52 PM, Dan Kirkwood wrote:
> 
> > Is there a better way to get to a class when you have the class name in  
> > a string?  
> > (This is rails-related, btw).. For instance, if I have a string clname  
> > = 'Train', and I want to access a class method Train#label. I can do  
> > this:
> > 
> > clname = 'Train'  
> > label = eval "#{clname}.label"
> > 
> > Is there a better way?
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Joey](https://avatars.discourse-cdn.com/v4/letter/j/f14d63/32.png) [@Joey](https://rubytalk.org/u/Joey)\
**Post date:** [31 May 2006 08:15 UTC](https://rubytalk.org/t/class-from-string/27868/5 "2006-05-31T08:15:17Z")

</div>

[http://www.eachmapinject.com/class\_name.html](http://www.eachmapinject.com/class_name.html)

j`ey  
[http://www.eachmapinject.com](http://www.eachmapinject.com) \<[http://www.eachmapinject.com/class\_name.html&gt](http://www.eachmapinject.com/class_name.html&gt);

$\_=%q{$\_=%q{Q};sub(/Q/,$\_);print};sub(/Q/,$\_);print

> **···**
>
> On 5/31/06, Ezra Zygmuntowicz \<ezmobius@gmail.com\> wrote:
> 
> > On May 30, 2006, at 4:52 PM, Dan Kirkwood wrote:
> > 
> > \> Is there a better way to get to a class when you have the class  
> > \> name in  
> > \> a string?  
> > \> (This is rails-related, btw).. For instance, if I have a string  
> > \> clname  
> > \> = 'Train', and I want to access a class method Train#label. I  
> > \> can do  
> > \> this:  
> > \>  
> > \> clname = 'Train'  
> > \> label = eval "#{clname}.label"  
> > \>  
> > \> Is there a better way?  
> > \>  
> > \> --  
> > \> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).  
> > \>
> > 
> > In rails you can use #constantize
> > 
> > clname = 'Train'  
> > clname.constantize.label
> > 
> > But you really should try to search before posting. This was asked a  
> > bunch of times in the last few weeks.
> > 
> > Cheers-  
> > -Ezra

---

<div class="post-metadata">

**Author:** ![Gregory\_Seidman](https://avatars.discourse-cdn.com/v4/letter/g/eb9ed0/32.png) [@Gregory\_Seidman](https://rubytalk.org/u/Gregory_Seidman)\
**Post date:** [31 May 2006 13:16 UTC](https://rubytalk.org/t/class-from-string/27868/6 "2006-05-31T13:16:19Z")

</div>

} \>Is there a better way to get to a class when you have the class name in  
} \>a string?  
}  
} This really gets asked a whole lot, especially from Ruby Forum. I  
} really, really, really wish there was a prominent FAQ on Ruby Forum  
} with this as like question number 1. This would save people like Dan  
} from having to ask the question and would save the rest of us from  
} having to answer it for the 50th time.

The problem with a simple FAQ is that the obvious answer (Object.const\_get)  
doesn't handle qualified classes (e.g. Foo::Bar) and the more complicated  
version usually given in response only handles fully-qualified classes. I  
decided to write a definitive version that handles fully- and  
partially-qualified class names, but it isn't trivial. Honestly, it should  
probably be part of Ruby's core. See  
[http://redcorundum.blogspot.com/2006/05/kernelqualifiedconstget.html](http://redcorundum.blogspot.com/2006/05/kernelqualifiedconstget.html)

} Ryan  
--Greg

> **···**
>
> On Wed, May 31, 2006 at 09:54:07AM +0900, Ryan Leavengood wrote:  
> } On 5/30/06, Dan Kirkwood \<[dangogh@gmail.com](mailto:dangogh@gmail.com)\> wrote:

---

<div class="post-metadata">

**Author:** ![Dan\_Kirkwood](https://avatars.discourse-cdn.com/v4/letter/d/cc9497/32.png) [@Dan\_Kirkwood](https://rubytalk.org/u/Dan_Kirkwood)\
**Post date:** [31 May 2006 15:38 UTC](https://rubytalk.org/t/class-from-string/27868/7 "2006-05-31T15:38:08Z")

</div>

Thanks all for the responses -- it's obvious this is a FAQ, but just try  
and filter thru all the cruft with the words 'class' and 'string'!! -dan

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

---

<div class="post-metadata">

**Author:** ![Mark\_Volkmann1](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@Mark\_Volkmann1](https://rubytalk.org/u/Mark_Volkmann1)\
**Post date:** [31 May 2006 13:41 UTC](https://rubytalk.org/t/class-from-string/27868/8 "2006-05-31T13:41:59Z")

</div>

> } \>Is there a better way to get to a class when you have the class name in  
> } \>a string?  
> }  
> } This really gets asked a whole lot, especially from Ruby Forum. I  
> } really, really, really wish there was a prominent FAQ on Ruby Forum  
> } with this as like question number 1. This would save people like Dan  
> } from having to ask the question and would save the rest of us from  
> } having to answer it for the 50th time.
> 
> The problem with a simple FAQ is that the obvious answer (Object.const\_get)  
> doesn't handle qualified classes (e.g. Foo::Bar) and the more complicated  
> version usually given in response only handles fully-qualified classes. I  
> decided to write a definitive version that handles fully- and  
> partially-qualified class names, but it isn't trivial. Honestly, it should  
> probably be part of Ruby's core.

Agreed! Has there already been a formal request to add this in 1.9?

> **···**
>
> On 5/31/06, Gregory Seidman \<gsslist+ruby@anthropohedron.net\> wrote:
> 
> > On Wed, May 31, 2006 at 09:54:07AM +0900, Ryan Leavengood wrote:  
> > } On 5/30/06, Dan Kirkwood \<dangogh@gmail.com\> wrote:
> 
> > See [Ruby, iOS, and Other Development: Kernel#qualified\_const\_get](http://redcorundum.blogspot.com/2006/05/kernelqualifiedconstget.html)
> 
> --  
> R. Mark Volkmann  
> Object Computing, Inc.

---

<div class="post-metadata">

**Author:** ![Olie\_D](https://avatars.discourse-cdn.com/v4/letter/o/f08c70/32.png) [@Olie\_D](https://rubytalk.org/u/Olie_D)\
**Post date:** [4 November 2007 02:39 UTC](https://rubytalk.org/t/class-from-string/27868/9 "2007-11-04T02:39:07Z")

</div>

Guest wrote:

> Thanks all for the responses -- it's obvious this is a FAQ, but just try  
> and filter thru all the cruft with the words 'class' and 'string'!! -dan

FWIW, I searched the Ruby forum for

&nbsp;&nbsp;&nbsp;"class from string"

and this rather helpful thread was the 1st one.

(Saved me from asking a FAQ :D)

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

---

<div class="post-metadata">

**Author:** ![Olie\_D](https://avatars.discourse-cdn.com/v4/letter/o/f08c70/32.png) [@Olie\_D](https://rubytalk.org/u/Olie_D)\
**Post date:** [4 November 2007 17:27 UTC](https://rubytalk.org/t/class-from-string/27868/10 "2007-11-04T17:27:14Z")

</div>

Olie D. wrote:

> Guest wrote:
> 
> > Thanks all for the responses -- it's obvious this is a FAQ, but just try  
> > and filter thru all the cruft with the words 'class' and 'string'!! -dan
> 
> FWIW, I searched the Ruby forum for
> 
> &nbsp;&nbsp;&nbsp;"class from string"
> 
> and this rather helpful thread was the 1st one.
> 
> (Saved me from asking a FAQ :D)

Bah! I was too quick on that -- now I'll probably end up asking FAQ'  
\<sigh\>

I'm trying to read-in a folder full of "plug-ins" and call each of them,  
in turn. Once I get the class-name, I do something like:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;require "#{PLUG\_IN\_DIR}/#{one\_plugin}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;plugin\_class=Object.const\_get(plugin\_class\_name).new  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;plugin\_class.some\_method

I \*thought\* that require worked a bit like the C pre-processor  
"include", in that it would read and execute the named file at that  
point, thereby defining my class and its methods. However, when I get  
to the middle line, I get

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;uninitialized constant PluginClassName

Since rails is mistaking my class-name for a constant, I'm guessing that  
require didn't execute the way I think it does, so my class-name isn't  
initialized.

...Or maybe I've completely mis-diagnosed the problem.

At any rate, can someone offer a suggestion for how to read a folder  
full of class-definition-files and, once at a time,

&nbsp;&nbsp;\* Execute the class definition, so that my app knows about it  
&nbsp;&nbsp;\* Instantiate an instance of the class (I think we have this part,  
above)  
&nbsp;&nbsp;\* Call a method on that class (should just be able to say  
"a\_class.a\_method", right?)

Thanks!

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