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.
Peter S. (Guest)
on 2007-02-23 03: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
Michael S. (Guest)
on 2007-02-23 04:28
(Received via mailing list)
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/
Peter S. (Guest)
on 2007-02-23 06:26
(Received via mailing list)
Demo:

my_class = "Person".camelize.constantize

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