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

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Peter S. (Guest)
on 2007-02-23 03:55
(Received via mailing list)

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

For example:
=> "Person"

I'd like to do the inverse:

=> 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.

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.


Michael S.
Peter S. (Guest)
on 2007-02-23 06:26
(Received via mailing list)

my_class = "Person".camelize.constantize

This topic is locked and can not be replied to.