From: Jim Weirich Date: 2002-03-10T06:22:35+09:00 Subject: Re: What classes exist >>>>> "David" == David Corbin writes: David> At the moment, I'm having problem with #2. In Java, you David> can do Class.forName("Foo") to see get a Class object for David> class Foo. The ruby Class class doesn't seem to have this David> capability. Please help Class names are constants. Top level class names are constants in Object. You can use const_get to fetch the value. For example ... foo_class = Object.const_get("Foo") If "Foo" is not a class, you will get a NameError in const_get. If the class is nested in a module (e.g. Foo::Bar), you have to do a little more work. The following code will return the class object for any class name (including nested classes), or will return nil if there is no match. def class_for(class_name) names = class_name.split("::") result = Object names.each { |n| result = result.const_get(n) } result rescue NameError nil end You can also use eval to get a constant value, but passing unvalidated strings to eval is kinda risky. -- -- Jim Weirich jweirich@one.net http://w3.one.net/~jweirich --------------------------------------------------------------------- "Beware of bugs in the above code; I have only proved it correct, not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)