On Apr 8, 10:13 ¨Âí¬ Çáòù ×òéçèô ¼ç÷ôí®®®Àíáã®ãïí÷òïôåº
> 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.  ¨Âêõóô äåðåîäïî ÷èåòùïõò ôåøô éó ãïíéîç æòïí>
> 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
>
>

Thanks for the tip - I *think* my data is well enough sanitized, but
you just never know so I may well use this approach.  Knowing how to
do things with Object.const_get is useful even if I don't use it.