Forum: Ruby on Rails [RoR],[C.L.R] How I convert a string object into a class?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
51cfbf34cf1f7ebb09c3a717e40c4a85?d=identicon&s=25 Peter Smith (Guest)
on 2007-02-23 02:55
(Received via mailing list)
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
Bce1d1b7c3ec7b577dcb42e254899e6b?d=identicon&s=25 Michael Schuerig (Guest)
on 2007-02-23 03:28
(Received via mailing list)
On Friday 23 February 2007, Peter Smith 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 Schuerig
mailto:michael@schuerig.de
http://www.schuerig.de/michael/
51cfbf34cf1f7ebb09c3a717e40c4a85?d=identicon&s=25 Peter Smith (Guest)
on 2007-02-23 05:26
(Received via mailing list)
Demo:

my_class = "Person".camelize.constantize

-Peter
This topic is locked and can not be replied to.