From: Ben Bleything Date: 2007-02-23T06:41:25+09:00 Subject: Re: Best way to get a class object from its name? On Fri, Feb 23, 2007, Wes Gamble wrote: > eval("#{requested_controller_class}.respond_to?('session_data_not_needed_by?')") > > where requested_controller_class is the name of an existing (controller) > class. > > Is the above code the best of way of invoking respond_to? on the class > in question, or is there some better way to get the actual class object > based solely on it's name? >> str = "ActiveRecord::Base" => "ActiveRecord::Base" >> str.constantize.respond_to? :new => true String#constantize is provided by Rails someplace. In non-Rails Ruby code, you could do this: >> Module.const_get( 'ActiveRecord' ).const_get( 'Base' ).respond_to? >> :find => true Of course, if it's a single-level, you only need one of those. Cheers, Ben