From: David Alan Black Date: 2001-11-17T03:04:02+09:00 Subject: [ruby-talk:25557] Re: Getting a class object from a string containg its name Hello -- On Sat, 17 Nov 2001, Lars Christensen wrote: > I need to get a reference to a class object from a string with the name of > the class. I want to do this in a secure way because the string comes from > an external source. I could not find any easy way to convert a string or > symbol to a class reference. I found two approaches though: You can use const_get. Here's a somewhat overwritten example :-) class ClassFinder def find_class(string) type.const_get(string) end end c = ClassFinder.new C = c.find_class("String") class C def thing puts "Hi!" end end "some string".thing # "Hi!" There are probably subtleties about scope and visibility and such that I'm not addressing.... David -- David Alan Black home: dblack@candle.superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav