On Apr 8, 10:13=A0pm, Gary Wright <gwtm... / mac.com> wrote:
> On Apr 8, 2009, at 4:40 PM, stephen O'D wrote:
>
> > dispatch =3D {
> > =A0'Foo' =3D> Foo,
> > =A0'Bar' =3D> Bar
> > }
>
> > obj =3D dispatch[var].new(...)
>
> This can sometimes be the best approach because if you simply =A0
> translate arbitrary text to a class name you are allowing arbitrary =A0
> classes to be instantiated. =A0It just depends on where your text is =A0
> coming from.
>
> Rick DeNatale recently posted in [ruby-talk:332670] this nice solution =
=A0
> to mapping text to class objects:
>
> > def constantize(camel_cased_word)
> > =A0 camel_cased_word.
> > =A0 =A0 sub(/^::/,'').
> > =A0 =A0 split("::").
> > =A0 =A0 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.