# String to Class?

**URL:** <https://www.ruby-forum.com/t/string-to-class/84125>\
**Category:** Rails\
**Created:** [February 1, 2007, 5:50pm UTC](https://www.ruby-forum.com/t/string-to-class/84125 "2007-02-01T17:50:07Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![starburger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/848f3c/32.png) [@starburger](https://www.ruby-forum.com/u/starburger)\
**Post date:** [February 1, 2007, 5:50pm UTC](https://www.ruby-forum.com/t/string-to-class/84125/1 "2007-02-01T17:50:07Z")

</div>

Hi,

I’m getting a class name as string via params[:class\_name]. How can I  
access the class?

E.g.

params[:class\_name] =\> “Post”

and then something like

params[:class\_name].to\_class.find(:first)

Why I want to to this? I want to create a generic commenting component  
with model controller and view (partials) that I can use to comment any  
other instance.

For now each class that can be commented (here: Article besides many  
others) needs rather the same action code:

class ArticleController \< ApplicationController

def comment  
@commentable = Article.find(params[:id]) # commentable  
@comment = Comment.new  
@comment.user = session[‘user’]  
@commentable.comments \<\< @comment  
if request.post?  
@comment = Comment.new(params[:comment])  
if @comment.save  
render :partial =\> “comment/comment\_list”  
else  
render :inline =\> “\<%= user\_message\_helper ‘comment’ %\>”,  
:status =\> 500  
end  
end  
end  
end

I would like to have this action only once in the CommentController,  
somehow like this:

class CommentController \< ApplicationController

def comment  
class\_name = params[:class\_name]  
@commentable = class\_name.to\_class.find(params[:id]) # commentable  
@comment = Comment.new  
@comment.user = session[‘user’]  
@commentable.comments \<\< @comment  
if request.post?  
@comment = Comment.new(params[:comment])  
if @comment.save  
render :partial =\> “comment/comment\_list”  
else  
render :inline =\> “\<%= user\_message\_helper ‘comment’ %\>”,  
:status =\> 500  
end  
end  
end  
end

I’m sure there is an elegant way to handle this…

---

<div class="post-metadata">

**Author:** ![starburger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/848f3c/32.png) [@starburger](https://www.ruby-forum.com/u/starburger)\
**Post date:** [February 1, 2007, 6:19pm UTC](https://www.ruby-forum.com/t/string-to-class/84125/2 "2007-02-01T18:19:55Z")

</div>

starburger wrote:

> Hi,
> 
> I’m getting a class name as string via params[:class\_name]. How can I  
> access the class?

I think what you want is eval

eval(“Ball”).find(:all)

for example will find all balls with a model class Ball. Not that a  
class name is case sensitive!!!

---

<div class="post-metadata">

**Author:** ![starburger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/848f3c/32.png) [@starburger](https://www.ruby-forum.com/u/starburger)\
**Post date:** [February 1, 2007, 6:20pm UTC](https://www.ruby-forum.com/t/string-to-class/84125/3 "2007-02-01T18:20:51Z")

</div>

Rob N. wrote:

> Not that a class name is case sensitive!!!

Should be:

Note that a class name is case sensitive!!!

---

<div class="post-metadata">

**Author:** ![starburger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/848f3c/32.png) [@starburger](https://www.ruby-forum.com/u/starburger)\
**Post date:** [February 1, 2007, 6:49pm UTC](https://www.ruby-forum.com/t/string-to-class/84125/4 "2007-02-01T18:49:46Z")

</div>

On 2/1/07, starburger [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> and then something like
> 
> params[:class\_name].to\_class.find(:first)

Try String#constantize:

params[:class\_name].constantize.find(:first)

Why I want to to this? I want to create a generic commenting component

---

<div class="post-metadata">

**Author:** ![starburger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/848f3c/32.png) [@starburger](https://www.ruby-forum.com/u/starburger)\
**Post date:** [February 1, 2007, 7:44pm UTC](https://www.ruby-forum.com/t/string-to-class/84125/5 "2007-02-01T19:44:54Z")

</div>

Thanks a lot for your help - you have solved my problem!

Starburger

---

<div class="post-metadata">

**Author:** ![starburger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/848f3c/32.png) [@starburger](https://www.ruby-forum.com/u/starburger)\
**Post date:** [February 1, 2007, 7:07pm UTC](https://www.ruby-forum.com/t/string-to-class/84125/6 "2007-02-01T19:07:50Z")

</div>

Michael G. wrote:

> On 2/1/07, starburger [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > and then something like
> > 
> > params[:class\_name].to\_class.find(:first)
> 
> Try String#constantize:
> 
> params[:class\_name].constantize.find(:first)
> 
> Why I want to to this? I want to create a generic commenting component

how bout eval(params[:class\_name].capitalize + “.find(:first)”) ?

---

<div class="post-metadata">

**Author:** ![starburger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/848f3c/32.png) [@starburger](https://www.ruby-forum.com/u/starburger)\
**Post date:** [February 1, 2007, 8:14pm UTC](https://www.ruby-forum.com/t/string-to-class/84125/7 "2007-02-01T20:14:53Z")

</div>

Stay away from eval.

Use String#constantize or Object#get\_const

Jason

---

<div class="post-metadata">

**Author:** ![starburger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/848f3c/32.png) [@starburger](https://www.ruby-forum.com/u/starburger)\
**Post date:** [February 1, 2007, 8:29pm UTC](https://www.ruby-forum.com/t/string-to-class/84125/8 "2007-02-01T20:29:55Z")

</div>

On 1-Feb-07, at 2:13 PM, Jason R. wrote:

> Starburger

Jason, Why stay away from eval?

I’m currently using eval to dynamically build a restful url\_helper  
reference. Is this also a bad case of using eval fro the same reason/?

ala: eval(“change\_state\_#{the\_model.class.name.downcase}\_url(#  
{the\_model.id})”)

(sorry to hijack - hopefully this adds to the discussion!)

Cheers,  
Jodi  
General Partner  
The nNovation Group inc.  
www.nnovation.ca/blog  
ï¿¼

---

<div class="post-metadata">

**Author:** ![starburger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/848f3c/32.png) [@starburger](https://www.ruby-forum.com/u/starburger)\
**Post date:** [February 1, 2007, 9:49pm UTC](https://www.ruby-forum.com/t/string-to-class/84125/9 "2007-02-01T21:49:07Z")

</div>

On Feb 1, 2007, at 11:28 AM, Jodi S. wrote:

> > 
> 
> {the\_model.id})")

Jodi-

The reason to stay away from eval is because you want to avoid  
evaling anything user generated. The OP is trying to make a class  
from an item in the params object. DANGER!! look here:

how bout eval(params[:class\_name].capitalize + “.find(:first)”) ?

Now anyone can try to enter all kinds of things in the params  
[:class\_name] form field. And it will get evaled without any checking  
on the server. Bad!

If you can avoid eval please do so at all costs. class\_Eval or  
instance\_eval with blocks instead of strings are better. But in your  
example:

eval(“change\_state\_#{the\_model.class.name.downcase}\_url(#  
{the\_model.id})”)

should be written like this:

send(“change\_state\_#{the\_model.class.name.downcase}\_url”, “#  
{the\_model.id}”)

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:** ![starburger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/848f3c/32.png) [@starburger](https://www.ruby-forum.com/u/starburger)\
**Post date:** [February 1, 2007, 9:48pm UTC](https://www.ruby-forum.com/t/string-to-class/84125/10 "2007-02-01T21:48:07Z")

</div>

On Thursday 01 February 2007 20:28, Jodi S. wrote:

> Why stay away from eval?

class SomethingsController \< ApplicationController  
def tricky  
…  
eval(params[:my\_param])  
…  
end  
end

Then consider what a request like this might do to your server

[http://example.com/trickies/tricky?my\_param=system("rm%20-rf%20%2F")](http://example.com/trickies/tricky?my_param=system(%22rm%20-rf%20%2F%22))

Michael

–  
Michael S.  
[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.schuerig.de/michael/](http://www.schuerig.de/michael/)

---

<div class="post-metadata">

**Author:** ![starburger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/848f3c/32.png) [@starburger](https://www.ruby-forum.com/u/starburger)\
**Post date:** [February 2, 2007, 5:50pm UTC](https://www.ruby-forum.com/t/string-to-class/84125/11 "2007-02-02T17:50:54Z")

</div>

> eval(“change\_state\_#{the\_model.class.name.downcase}\_url(#  
> {the\_model.id})”)
> 
> should be written like this:
> 
> send(“change\_state\_#{the\_model.class.name.downcase}\_url”, “#  
> {the\_model.id}”)

Hey Ezra -

the send didn’t succeed as :  
undefined method `change\_state\_project\_url(:id =\> 85, :event =\>  
‘change\_project\_info’)’ for #\<#Class:0x345af20:0x345aef8\>

note: this code is running inside application\_helper - not sure what  
self is inside a helper, but it doesn’t know about rest url helpers.

Do you know what object ‘owns’ these methods? I gather they are Class  
methods.

thanx.  
Jodi

---

<div class="post-metadata">

**Author:** ![starburger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/848f3c/32.png) [@starburger](https://www.ruby-forum.com/u/starburger)\
**Post date:** [February 1, 2007, 11:57pm UTC](https://www.ruby-forum.com/t/string-to-class/84125/12 "2007-02-01T23:57:27Z")

</div>

On 1-Feb-07, at 3:48 PM, Ezra Z. wrote:

> > > Jason  
> > > I’m currently using eval to dynamically build a restful url\_helper  
> > > General Partner  
> > > evaling anything user generated. The OP is trying to make a class  
> > > instance\_eval with blocks instead of strings are better. But in your  
> > > Cheers-
> 
> – Ezra Z.  
> – Lead Rails Evangelist  
> – removed\_email\_address@domain.invalid  
> – Engine Y., Serious Rails Hosting  
> – (866) 518-YARD (9273)

Thanx Ezra and Michael.

I understood that Send limitted the scope(object calls) of damage  
that can be cause by eval - and a object.send("#{params[:command]) is  
dangerous enough. Whereby Eval’s scope isn’t limited.

In my particular case the\_model is instantiate by the controller  
action - so eval would likely be as ‘safe’ as send (?).

My questions was questioning what additional protection that send  
provides - is there any protection provided above and beyond the  
localization of the class executing the arbritary code?

I think my learning here is that regardless of means(send, eval,  
etc), that additionally checking should be done on the methods being  
called - limiting the scope of the methods that can be called (I’ve  
recently used acts\_as\_state\_machine to limit sends based upon model  
state and other guards - this provides funnel that sends must qualify  
before running - and still give me the benefit of a measure of dynamism.

ala:  
instance.send("#{params[:command]}") if  
Class.legitimate\_commands.include?(params[:command])

thanks for teaching this ruby-nuby.  
Jodi

---

<div class="post-metadata">

**Author:** ![starburger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/848f3c/32.png) [@starburger](https://www.ruby-forum.com/u/starburger)\
**Post date:** [February 3, 2007, 12:49am UTC](https://www.ruby-forum.com/t/string-to-class/84125/13 "2007-02-03T00:49:41Z")

</div>

As everybody has said, avoid eval - use const\_get to retrieve a  
constant. If you want a class from a string, here’s a simple  
implementation:

class String  
def to\_class  
Object.const\_get(self)  
end  
end
