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

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

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:[email protected]
http://www.schuerig.de/michael/

Demo:

my_class = “Person”.camelize.constantize

-Peter

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs