From: Stefano Crocco Date: 2008-04-14T21:37:44+09:00 Subject: Re: Dynamically referring to a Class On Monday 14 April 2008, Gaudi Mi wrote: > I have several classes in my application, e.g. Person, Employer, Office. > And obviously I have some Class methods for each, e.g. Person.find. > > At one point in my application I will be referencing the Class name > dynamically, I will have it in a String. So e.g. class = "Person". I > want to send the Person class the 'find' message (call Person.find). > How do I do this in Ruby. I've scoured the API and Google but it's a > hard thing to search for. > > Thanks. Object.const_get(cls).find If the class is defined in a module, replace Object with the name of the module. For example: module M class C def self.find end end end M.const_get('C').find I hope this helps Stefano