# Anything better than klass = eval("#{task\_class}")

**URL:** <https://rubytalk.org/t/anything-better-than-klass-eval-task-class/52608>\
**Category:** ruby-talk\
**Created:** [1 April 2009 13:14 UTC](https://rubytalk.org/t/anything-better-than-klass-eval-task-class/52608 "2009-04-01T13:14:42Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paganoni](https://avatars.discourse-cdn.com/v4/letter/p/91b2a8/32.png) [@Paganoni](https://rubytalk.org/u/Paganoni)\
**Post date:** [1 April 2009 13:14 UTC](https://rubytalk.org/t/anything-better-than-klass-eval-task-class/52608/1 "2009-04-01T13:14:42Z")

</div>

Hi, well subject is self explanatory...

Seems a bit to verbose for me but I don't know any other solution to convert a string containing a class name to a constant of that class name... Excepted rails way : task\_class.camelize.constantize which is verbose too...

---

<div class="post-metadata">

**Author:** ![Greg\_Brown1](https://avatars.discourse-cdn.com/v4/letter/g/258eb7/32.png) [@Greg\_Brown1](https://rubytalk.org/u/Greg_Brown1)\
**Post date:** [1 April 2009 13:32 UTC](https://rubytalk.org/t/anything-better-than-klass-eval-task-class/52608/2 "2009-04-01T13:32:49Z")

</div>

=\> String

-greg

> **···**
>
> On Wed, Apr 1, 2009 at 9:14 AM, Paganoni \<noway@fakenullvoid.com\> wrote:
> 
> > Hi, well subject is self explanatory...
> > 
> > Seems a bit to verbose for me but I don't know any other solution to convert  
> > a string containing a class name to a constant of that class name...  
> > Excepted rails way : task\_class.camelize.constantize which is verbose too...
> 
> > > Object.const\_get("String")
> 
> --  
> BOOK: [http://rubybestpractices.com](http://rubybestpractices.com)  
> TECH: [http://blog.majesticseacreature.com](http://blog.majesticseacreature.com)  
> NON-TECH: [http://metametta.blogspot.com](http://metametta.blogspot.com)

---

<div class="post-metadata">

**Author:** ![Rob\_Biedenharn1](https://avatars.discourse-cdn.com/v4/letter/r/57b2e6/32.png) [@Rob\_Biedenharn1](https://rubytalk.org/u/Rob_Biedenharn1)\
**Post date:** [1 April 2009 14:47 UTC](https://rubytalk.org/t/anything-better-than-klass-eval-task-class/52608/3 "2009-04-01T14:47:08Z")

</div>

Or dealing with things like Admin::User or Net::HTTP

&nbsp;&nbsp;&nbsp;# from Jim Weirich (based on email correspondence)  
&nbsp;&nbsp;&nbsp;def constantize(camel\_cased\_word)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;camel\_cased\_word.  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sub(/^::/,'').  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;split("::").  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;inject(Object) { |scope, name| scope.const\_get(name) }  
&nbsp;&nbsp;&nbsp;end

It may be more verbose, but it is much safer that plain 'ole eval()

-Rob

Rob Biedenharn [http://agileconsultingllc.com](http://agileconsultingllc.com)  
Rob@AgileConsultingLLC.com

> **···**
>
> On Apr 1, 2009, at 9:32 AM, Gregory Brown wrote:
> 
> > On Wed, Apr 1, 2009 at 9:14 AM, Paganoni \<noway@fakenullvoid.com\> \> wrote:
> > 
> > > Hi, well subject is self explanatory...
> > > 
> > > Seems a bit to verbose for me but I don't know any other solution to convert  
> > > a string containing a class name to a constant of that class name...  
> > > Excepted rails way : task\_class.camelize.constantize which is verbose too...
> > 
> > > > Object.const\_get("String")
> > 
> > =\> String
> > 
> > -greg
> > 
> > --  
> > BOOK: [http://rubybestpractices.com](http://rubybestpractices.com)  
> > TECH: [http://blog.majesticseacreature.com](http://blog.majesticseacreature.com)  
> > NON-TECH: [http://metametta.blogspot.com](http://metametta.blogspot.com)

---

<div class="post-metadata">

**Author:** ![Rick\_DeNatale1](https://avatars.discourse-cdn.com/v4/letter/r/bbce88/32.png) [@Rick\_DeNatale1](https://rubytalk.org/u/Rick_DeNatale1)\
**Post date:** [1 April 2009 16:09 UTC](https://rubytalk.org/t/anything-better-than-klass-eval-task-class/52608/4 "2009-04-01T16:09:09Z")

</div>

Actually, I don't think this is entirely right, since const\_get will find  
constants found in outer scopes.

For example constantize("MyModule::Object") would return ::Object

So a little better would be:

def constantize(camel\_cased\_word)  
&nbsp;&nbsp;&nbsp;camel\_cased\_word.  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sub(/^::/,'').  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;split("::").  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;inject(Object) { |scope, name| scope.const\_defined?(name) ?  
scope.const\_get(name) : scope.const\_missing(name) }  
end

> **···**
>
> On Wed, Apr 1, 2009 at 10:47 AM, Rob Biedenharn \<Rob@agileconsultingllc.com\>wrote:
> 
> > On Apr 1, 2009, at 9:32 AM, Gregory Brown wrote:
> > 
> > On Wed, Apr 1, 2009 at 9:14 AM, Paganoni \<noway@fakenullvoid.com\> wrote:
> > 
> > > > Hi, well subject is self explanatory...
> > > > 
> > > > Seems a bit to verbose for me but I don't know any other solution to  
> > > > convert  
> > > > a string containing a class name to a constant of that class name...  
> > > > Excepted rails way : task\_class.camelize.constantize which is verbose  
> > > > too...
> > > 
> > > Object.const\_get("String")
> > > 
> > > > > 
> > > > 
> > > > =\> String
> > > 
> > > -greg
> > > 
> > > --  
> > > BOOK: [http://rubybestpractices.com](http://rubybestpractices.com)  
> > > TECH: [http://blog.majesticseacreature.com](http://blog.majesticseacreature.com)  
> > > NON-TECH: [http://metametta.blogspot.com](http://metametta.blogspot.com)
> > 
> > Or dealing with things like Admin::User or Net::HTTP
> > 
> > # from Jim Weirich (based on email correspondence)  
> > def constantize(camel\_cased\_word)  
> > &nbsp;&nbsp;&nbsp;camel\_cased\_word.  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sub(/^::/,'').  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;split("::").  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;inject(Object) { |scope, name| scope.const\_get(name) }  
> > end
> > 
> > It may be more verbose, but it is much safer that plain 'ole eval()
> 
> --  
> Rick DeNatale
> 
> Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
> Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
> WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
> LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)

---

<div class="post-metadata">

**Author:** ![Paganoni](https://avatars.discourse-cdn.com/v4/letter/p/91b2a8/32.png) [@Paganoni](https://rubytalk.org/u/Paganoni)\
**Post date:** [2 April 2009 06:30 UTC](https://rubytalk.org/t/anything-better-than-klass-eval-task-class/52608/5 "2009-04-02T06:30:38Z")

</div>

Okay, so nothing less verbose 😉 But thanks for all the answers...

> **···**
>
> le 01/04/2009 16:47, Rob Biedenharn nous a dit:
> 
> > On Apr 1, 2009, at 9:32 AM, Gregory Brown wrote:
> > 
> > > On Wed, Apr 1, 2009 at 9:14 AM, Paganoni \<noway@fakenullvoid.com\> \>\> wrote:
> > > 
> > > > Hi, well subject is self explanatory...
> > > > 
> > > > Seems a bit to verbose for me but I don't know any other solution to convert  
> > > > a string containing a class name to a constant of that class name...  
> > > > Excepted rails way : task\_class.camelize.constantize which is verbose too...
> > > > 
> > > > > Object.const\_get("String")
> > > 
> > > =\> String
> > 
> > Or dealing with things like Admin::User or Net::HTTP
> > 
> > &nbsp;&nbsp;&nbsp;# from Jim Weirich (based on email correspondence)  
> > &nbsp;&nbsp;&nbsp;def constantize(camel\_cased\_word)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;camel\_cased\_word.  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sub(/^::/,'').  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;split("::").  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;inject(Object) { |scope, name| scope.const\_get(name) }  
> > &nbsp;&nbsp;&nbsp;end
> > 
> > It may be more verbose, but it is much safer that plain 'ole eval()

---

<div class="post-metadata">

**Author:** ![Sean\_O\_Halpin](https://avatars.discourse-cdn.com/v4/letter/s/ac91a4/32.png) [@Sean\_O\_Halpin](https://rubytalk.org/u/Sean_O_Halpin)\
**Post date:** [1 April 2009 16:31 UTC](https://rubytalk.org/t/anything-better-than-klass-eval-task-class/52608/6 "2009-04-01T16:31:12Z")

</div>

The const\_missing handling is a nice touch. I'm pinching this 🙂

> **···**
>
> On Wed, Apr 1, 2009 at 5:09 PM, Rick DeNatale \<rick.denatale@gmail.com\> wrote:
> 
> > Actually, I don't think this is entirely right, since const\_get will find  
> > constants found in outer scopes.
> > 
> > For example constantize("MyModule::Object") would return ::Object
> > 
> > So a little better would be:
> > 
> > def constantize(camel\_cased\_word)  
> > camel\_cased\_word.  
> > sub(/^::/,'').  
> > split("::").  
> > inject(Object) { |scope, name| scope.const\_defined?(name) ?  
> > scope.const\_get(name) : scope.const\_missing(name) }  
> > end

---

<div class="post-metadata">

**Author:** ![Rob\_Biedenharn1](https://avatars.discourse-cdn.com/v4/letter/r/57b2e6/32.png) [@Rob\_Biedenharn1](https://rubytalk.org/u/Rob_Biedenharn1)\
**Post date:** [1 April 2009 16:37 UTC](https://rubytalk.org/t/anything-better-than-klass-eval-task-class/52608/7 "2009-04-01T16:37:03Z")

</div>

> > > > Hi, well subject is self explanatory...
> > > > 
> > > > Seems a bit to verbose for me but I don't know any other solution to  
> > > > convert  
> > > > a string containing a class name to a constant of that class name...  
> > > > Excepted rails way : task\_class.camelize.constantize which is verbose  
> > > > too...
> > > 
> > > Object.const\_get("String")
> > > 
> > > > > 
> > > > 
> > > > =\> String
> > > 
> > > -greg
> > > 
> > > --  
> > > BOOK: [http://rubybestpractices.com](http://rubybestpractices.com)  
> > > TECH: [http://blog.majesticseacreature.com](http://blog.majesticseacreature.com)  
> > > NON-TECH: [http://metametta.blogspot.com](http://metametta.blogspot.com)
> > 
> > Or dealing with things like Admin::User or Net::HTTP
> > 
> > # from Jim Weirich (based on email correspondence)  
> > def constantize(camel\_cased\_word)  
> > &nbsp;&nbsp;camel\_cased\_word.  
> > &nbsp;&nbsp;&nbsp;&nbsp;sub(/^::/,'').  
> > &nbsp;&nbsp;&nbsp;&nbsp;split("::").  
> > &nbsp;&nbsp;&nbsp;&nbsp;inject(Object) { |scope, name| scope.const\_get(name) }  
> > end
> > 
> > It may be more verbose, but it is much safer that plain 'ole eval()
> 
> Actually, I don't think this is entirely right, since const\_get will find  
> constants found in outer scopes.
> 
> For example constantize("MyModule::Object") would return ::Object
> 
> So a little better would be:
> 
> def constantize(camel\_cased\_word)  
> &nbsp;&nbsp;camel\_cased\_word.  
> &nbsp;&nbsp;&nbsp;&nbsp;sub(/^::/,'').  
> &nbsp;&nbsp;&nbsp;&nbsp;split("::").  
> &nbsp;&nbsp;&nbsp;&nbsp;inject(Object) { |scope, name| scope.const\_defined?(name) ?  
> scope.const\_get(name) : scope.const\_missing(name) }  
> end
> 
> --  
> Rick DeNatale
> 
> Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
> Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
> WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
> LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)

> &nbsp;&nbsp;# from Jim Weirich (based on email correspondence)

?\> def constantize(camel\_cased\_word)

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;camel\_cased\_word.

?\> sub(/^::/,'').  
?\> split("::").  
?\> inject(Object) { |scope, name| scope.const\_get(name) }

> &nbsp;&nbsp;&nbsp;&nbsp;end

=\> nil

> constantize("MyModule::Object")

NameError: uninitialized constant MyModule  
&nbsp;&nbsp;from (irb):6:in `const\_get'  
&nbsp;&nbsp;from (irb):6:in `constantize'  
&nbsp;&nbsp;from (irb):8:in `inject'  
&nbsp;&nbsp;from (irb):3:in `each'  
&nbsp;&nbsp;from (irb):3:in `inject'  
&nbsp;&nbsp;from (irb):3:in `constantize'  
&nbsp;&nbsp;from (irb):8

> def constantize(camel\_cased\_word)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;camel\_cased\_word.

?\> sub(/^::/,'').  
?\> split("::").  
?\> inject(Object) { |scope, name| scope.const\_defined?(name) ? scope.const\_get(name) : scope.const\_missing(name) }

> &nbsp;&nbsp;&nbsp;&nbsp;end

=\> nil

> constantize("MyModule::Object")

NameError: uninitialized constant MyModule  
&nbsp;&nbsp;from (irb):14:in `constantize'  
&nbsp;&nbsp;from (irb):16:in `inject'  
&nbsp;&nbsp;from (irb):11:in `each'  
&nbsp;&nbsp;from (irb):11:in `inject'  
&nbsp;&nbsp;from (irb):11:in `constantize'  
&nbsp;&nbsp;from (irb):16

Except for the slight difference in the backtrace, it looks the same to me. Why do you think it will return Object for constantize("MyModule::Object") ?

-Rob

Rob Biedenharn [http://agileconsultingllc.com](http://agileconsultingllc.com)  
Rob@AgileConsultingLLC.com

> **···**
>
> On Apr 1, 2009, at 12:09 PM, Rick DeNatale wrote:
> 
> > On Wed, Apr 1, 2009 at 10:47 AM, Rob Biedenharn \> \<Rob@agileconsultingllc.com\>wrote:
> > 
> > > On Apr 1, 2009, at 9:32 AM, Gregory Brown wrote:  
> > > On Wed, Apr 1, 2009 at 9:14 AM, Paganoni \<noway@fakenullvoid.com\> \>\> wrote:
> 
> &nbsp;&nbsp;from :0

---

<div class="post-metadata">

**Author:** ![Greg\_Brown1](https://avatars.discourse-cdn.com/v4/letter/g/258eb7/32.png) [@Greg\_Brown1](https://rubytalk.org/u/Greg_Brown1)\
**Post date:** [2 April 2009 14:17 UTC](https://rubytalk.org/t/anything-better-than-klass-eval-task-class/52608/8 "2009-04-02T14:17:06Z")

</div>

But all of the mentioned suggestions are much better than  
eval("ConstantName") in security, performance, and general design.  
So if you want something less verbose, just make a method like  
C("Whatever") that uses one of those implementations, rather than  
forcing Ruby to fire up its parser via eval() just to get a constant.

> **···**
>
> On Thu, Apr 2, 2009 at 2:30 AM, Paganoni \<noway@fakenullvoid.com\> wrote:
> 
> > le 01/04/2009 16:47, Rob Biedenharn nous a dit:
> > 
> > > On Apr 1, 2009, at 9:32 AM, Gregory Brown wrote:
> > > 
> > > > On Wed, Apr 1, 2009 at 9:14 AM, Paganoni \<noway@fakenullvoid.com\> wrote:
> > > > 
> > > > > Hi, well subject is self explanatory...
> > > > > 
> > > > > Seems a bit to verbose for me but I don't know any other solution to  
> > > > > convert  
> > > > > a string containing a class name to a constant of that class name...  
> > > > > Excepted rails way : task\_class.camelize.constantize which is verbose  
> > > > > too...
> > > > > 
> > > > > > Object.const\_get("String")
> > > > 
> > > > =\> String
> > > 
> > > Or dealing with things like Admin::User or Net::HTTP
> > > 
> > > # from Jim Weirich (based on email correspondence)  
> > > def constantize(camel\_cased\_word)  
> > > camel\_cased\_word.  
> > > sub(/^::/,'').  
> > > split("::").  
> > > inject(Object) { |scope, name| scope.const\_get(name) }  
> > > end
> > > 
> > > It may be more verbose, but it is much safer that plain 'ole eval()
> > 
> > Okay, so nothing less verbose 😉 But thanks for all the answers...

---

<div class="post-metadata">

**Author:** ![Rick\_DeNatale1](https://avatars.discourse-cdn.com/v4/letter/r/bbce88/32.png) [@Rick\_DeNatale1](https://rubytalk.org/u/Rick_DeNatale1)\
**Post date:** [1 April 2009 18:24 UTC](https://rubytalk.org/t/anything-better-than-klass-eval-task-class/52608/9 "2009-04-01T18:24:56Z")

</div>

Because it does, if MyModule is actually defined and doesn't define another  
inner constant called Object:

def constantize(camel\_cased\_word)  
&nbsp;&nbsp;camel\_cased\_word.

&nbsp;&nbsp;sub(/^::/,'').  
&nbsp;&nbsp;split("::").  
&nbsp;&nbsp;inject(Object) { |scope, name| scope.const\_get(name) }  
end

module MyModule  
end

constantize("MyModule::Object") # =\> Object

> **···**
>
> On Wed, Apr 1, 2009 at 12:37 PM, Rob Biedenharn \<Rob@agileconsultingllc.com\>wrote:
> 
> > On Apr 1, 2009, at 12:09 PM, Rick DeNatale wrote:
> > 
> > On Wed, Apr 1, 2009 at 10:47 AM, Rob Biedenharn \>\> \<Rob@agileconsultingllc.com\>wrote:
> > 
> > > On Apr 1, 2009, at 9:32 AM, Gregory Brown wrote:
> > > 
> > > > On Wed, Apr 1, 2009 at 9:14 AM, Paganoni \<noway@fakenullvoid.com\> wrote:
> > > > 
> > > > > Hi, well subject is self explanatory...
> > > > > 
> > > > > > Seems a bit to verbose for me but I don't know any other solution to  
> > > > > > convert  
> > > > > > a string containing a class name to a constant of that class name...  
> > > > > > Excepted rails way : task\_class.camelize.constantize which is verbose  
> > > > > > too...
> > > > > 
> > > > > Object.const\_get("String")
> > > > > 
> > > > > > > =\> String
> > > > > 
> > > > > -greg
> > > > > 
> > > > > --  
> > > > > BOOK: [http://rubybestpractices.com](http://rubybestpractices.com)  
> > > > > TECH: [http://blog.majesticseacreature.com](http://blog.majesticseacreature.com)  
> > > > > NON-TECH: [http://metametta.blogspot.com](http://metametta.blogspot.com)
> > > > 
> > > > Or dealing with things like Admin::User or Net::HTTP
> > > > 
> > > > # from Jim Weirich (based on email correspondence)  
> > > > def constantize(camel\_cased\_word)  
> > > > camel\_cased\_word.  
> > > > &nbsp;&nbsp;&nbsp;sub(/^::/,'').  
> > > > &nbsp;&nbsp;&nbsp;split("::").  
> > > > &nbsp;&nbsp;&nbsp;inject(Object) { |scope, name| scope.const\_get(name) }  
> > > > end
> > > > 
> > > > It may be more verbose, but it is much safer that plain 'ole eval()
> > > 
> > > Actually, I don't think this is entirely right, since const\_get will find  
> > > constants found in outer scopes.
> > > 
> > > For example constantize("MyModule::Object") would return ::Object
> > > 
> > > So a little better would be:
> > > 
> > > def constantize(camel\_cased\_word)  
> > > camel\_cased\_word.  
> > > &nbsp;&nbsp;&nbsp;sub(/^::/,'').  
> > > &nbsp;&nbsp;&nbsp;split("::").  
> > > &nbsp;&nbsp;&nbsp;inject(Object) { |scope, name| scope.const\_defined?(name) ?  
> > > scope.const\_get(name) : scope.const\_missing(name) }  
> > > end
> > > 
> > > Except for the slight difference in the backtrace, it looks the same to
> > 
> > me. Why do you think it will return Object for  
> > constantize("MyModule::Object") ?

---

<div class="post-metadata">

**Author:** ![Rob\_Biedenharn1](https://avatars.discourse-cdn.com/v4/letter/r/57b2e6/32.png) [@Rob\_Biedenharn1](https://rubytalk.org/u/Rob_Biedenharn1)\
**Post date:** [1 April 2009 19:28 UTC](https://rubytalk.org/t/anything-better-than-klass-eval-task-class/52608/10 "2009-04-01T19:28:51Z")

</div>

Ah, I see now. Thanks for the improvement, Rick!

-Rob

Rob Biedenharn [http://agileconsultingllc.com](http://agileconsultingllc.com)  
Rob@AgileConsultingLLC.com

> **···**
>
> On Apr 1, 2009, at 2:24 PM, Rick DeNatale wrote:
> 
> > Because it does, if MyModule is actually defined and doesn't define another  
> > inner constant called Object:
