From: Robert Dober Date: 2008-04-14T23:31:46+09:00 Subject: Re: Dynamically referring to a Class On Mon, Apr 14, 2008 at 2:32 PM, 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. > -- > Posted via http://www.ruby-forum.com/. > > Hmm others have told you how to do what you wanted. I however wonder if the need of doing metaprogramming to find your classes is really a good sign for your design. If the following is feasible for you I might suggest something like the following MyClasses = { "Person" => Person, "Animal" => Animal ... } or even MyClasses={} MyClasses["Person"] = Class::new { } MyClasses["Animal"] = Class::new { } In other words do not store away something just to get it back again ;) HTH Robert -- http://ruby-smalltalk.blogspot.com/ --- Whereof one cannot speak, thereof one must be silent. Ludwig Wittgenstein