From: Gary Wright Date: 2009-04-09T06:13:39+09:00 Subject: Re: Instantiate different class based on variable contents On Apr 8, 2009, at 4:40 PM, stephen O'D wrote: > dispatch = { > 'Foo' => Foo, > 'Bar' => Bar > } > > obj = dispatch[var].new(...) This can sometimes be the best approach because if you simply translate arbitrary text to a class name you are allowing arbitrary classes to be instantiated. It just depends on where your text is coming from. Rick DeNatale recently posted in [ruby-talk:332670] this nice solution to mapping text to class objects: > def constantize(camel_cased_word) > camel_cased_word. > sub(/^::/,''). > split("::"). > inject(Object) { |scope, name| scope.const_defined?(name) ? > scope.const_get(name) : scope.const_missing(name) } > end