In article <9e9c2a$1r8$1 / wanadoo.fr>,
Benoit Cerrina <benoit.cerrina / writeme.com> wrote:
>Hi,
>I've been retrieving a Class object by its class name by doing:
>
> def XmlElem.getClass4Name(name)
>  if(eval("defined? #{name} and #{name}.type == Class "))
>   return eval("#name")
>  else
>   return nil
>  end
> end
>
>it seems to work but is it the best way, I try to avoid using eval normally.

I don't like over using eval either...

You could use Module.const_get to do this since class names are constants.
So, if you wanted to get a class called "MyKlass":
instance = Module.const_get("MyKlass").new
would do it.  However, in the case where you need to get at a class 
defined in another class (such that the class hierarchy has "::" in it, 
you'll have to do something in addition.  Look up "dynamic class 
instantiation" in deja.com under the comp.lang.ruby newsgroup - as I 
recall there is some code there for handling this case.

Phil