[RoR],[C.L.R] How I convert a string object into a class?


#1

People,

Lately I’ve been using the .name() method of classes.

For example:

Person.name
=> “Person”

Today…
I’d like to do the inverse:

“Person”.to_class
=> Person

How do I transform a string into a class?
I’m working with a Rails app where I need to
get names of AR classes at run-time.

-Peter


#2

On Friday 23 February 2007, Peter S. wrote:

“Person”.to_class
=> Person

Has been discussed over and over. Look in the API docs for #constantize
and search this mailing list for it.

HTH,
Michael


Michael S.
mailto:removed_email_address@domain.invalid
http://www.schuerig.de/michael/


#3

Demo:

my_class = “Person”.camelize.constantize

-Peter