From: Paganoni Date: 2009-04-02T15:30:38+09:00 Subject: Re: Anything better than klass = eval("#{task_class}") le 01/04/2009 16:47, Rob Biedenharn nous a dit: > On Apr 1, 2009, at 9:32 AM, Gregory Brown wrote: > >> On Wed, Apr 1, 2009 at 9:14 AM, Paganoni >> wrote: >>> Hi, well subject is self explanatory... >>> >>> Seems a bit to verbose for me but I don't know any other solution >>> to convert >>> a string containing a class name to a constant of that class name... >>> Excepted rails way : task_class.camelize.constantize which is >>> verbose too... >>>> Object.const_get("String") >> => String >> > > Or dealing with things like Admin::User or Net::HTTP > > # from Jim Weirich (based on email correspondence) > def constantize(camel_cased_word) > camel_cased_word. > sub(/^::/,''). > split("::"). > inject(Object) { |scope, name| scope.const_get(name) } > end > > It may be more verbose, but it is much safer that plain 'ole eval() > Okay, so nothing less verbose ;-) But thanks for all the answers...