# String to Class?

**URL:** <https://www.ruby-forum.com/t/string-to-class/130490>\
**Category:** Ruby\
**Created:** [February 28, 2008, 6:31pm UTC](https://www.ruby-forum.com/t/string-to-class/130490 "2008-02-28T18:31:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aaron\_T](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e79b87/32.png) [@Aaron\_T](https://www.ruby-forum.com/u/Aaron_T)\
**Post date:** [February 28, 2008, 6:31pm UTC](https://www.ruby-forum.com/t/string-to-class/130490/1 "2008-02-28T18:31:32Z")

</div>

I want to be able to take a class name stored in a variable as a string  
like:

foo = “Foo::Bar”

and convert that to the class Foo::Bar so that I can call .new(), etc:

obj = foo.new

I know I can just do:

foo = Foo::Bar

but that doesn’t solve my problem which is to accept the class name as  
an argument on the command line from the user.

Thanks,  
Aaron

---

<div class="post-metadata">

**Author:** ![Aaron\_T](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e79b87/32.png) [@Aaron\_T](https://www.ruby-forum.com/u/Aaron_T)\
**Post date:** [February 28, 2008, 6:42pm UTC](https://www.ruby-forum.com/t/string-to-class/130490/2 "2008-02-28T18:42:46Z")

</div>

On Thu, Feb 28, 2008 at 11:28 AM, Aaron T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> foo = Foo::Bar  
> [http://tcpreplay.synfin.net/](http://tcpreplay.synfin.net/) - Pcap editing & replay tools for Unix  
> They that can give up essential liberty to obtain a little temporary  
> safety deserve neither liberty nor safety. – Benjamin Franklin

I’m sure there’s a better way, but:

module Foo  
class Bar

end  
end

foo = “Foo::Bar”  
foo = foo.split(“::”).inject(Object){|s,v| s.const\_get(v) }

obj = foo.new

p obj

---

<div class="post-metadata">

**Author:** ![Aaron\_T](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e79b87/32.png) [@Aaron\_T](https://www.ruby-forum.com/u/Aaron_T)\
**Post date:** [February 28, 2008, 6:49pm UTC](https://www.ruby-forum.com/t/string-to-class/130490/3 "2008-02-28T18:49:20Z")

</div>

Alle Thursday 28 February 2008, Aaron T. ha scritto:

> foo = Foo::Bar
> 
> but that doesn’t solve my problem which is to accept the class name as  
> an argument on the command line from the user.
> 
> Thanks,  
> Aaron

foo.split(’::’).inject(Kernel){|res, i| res.const\_get(i)}

Stefano

---

<div class="post-metadata">

**Author:** ![Aaron\_T](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e79b87/32.png) [@Aaron\_T](https://www.ruby-forum.com/u/Aaron_T)\
**Post date:** [February 29, 2008, 12:52am UTC](https://www.ruby-forum.com/t/string-to-class/130490/4 "2008-02-29T00:52:51Z")

</div>

Stefano C. wrote:

> foo.split(’::’).inject(Kernel){|res, i| res.const\_get(i)}
> 
> Stefano

Gordon, Stefano,

That is a SUPER cool form of inject()! I usually just use is as a  
builder for a hash or other container type, but having inject using  
something fairly different on each iteration is grand! Why didn’t I  
think of that???!!!

Great tip, thanks again

ilan

---

<div class="post-metadata">

**Author:** ![Aaron\_T](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e79b87/32.png) [@Aaron\_T](https://www.ruby-forum.com/u/Aaron_T)\
**Post date:** [February 29, 2008, 1:42am UTC](https://www.ruby-forum.com/t/string-to-class/130490/5 "2008-02-29T01:42:21Z")

</div>

cool~ I haven’t thought it before.

Rails have its owner ext for this function classed  
Inflector.constantize.  
And it’s implementation is used module\_eval.  
So it can also be :

obj = Object.module\_eval(foo).new

On Fri, Feb 29, 2008 at 1:47 AM, Stefano C.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Aaron\_T](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e79b87/32.png) [@Aaron\_T](https://www.ruby-forum.com/u/Aaron_T)\
**Post date:** [February 29, 2008, 1:18am UTC](https://www.ruby-forum.com/t/string-to-class/130490/6 "2008-02-29T01:18:17Z")

</div>

On Feb 28, 2008, at 12:47 PM, Stefano C. wrote:

> foo.split(’::’).inject(Kernel){|res, i| res.const\_get(i)}

That is the coolest thing i have ever seen. Seriously. It will take a  
lot of awesome to beat this!

+1

\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_|

- Ari  
I just bought another case of RockStar. Expect architectural changes.
