Alle marted13 marzo 2007, Matteo Cavalleri ha scritto:
> Christian Surlykke <christian / surlykke.dk> wrote:
> > Object.const_get can convert a String to a constant, so if you do:
> >
> >       Object.const_get(h[:class].to_s).new(h)
> >
> > it should work, no matter if the hash comes from source or yaml
>
> the idea was excellent but it seems it doens't work if the class I need
> is defined in another class namespace, e.g.
>
> irb(main):117:0> Object.const_get('CGI::Cookie')
> NameError: wrong constant name CGI::Cookie
>
> anyway thanks for the help

Can't you store the class name (using Class#name) instead of the class itself 
in the yaml file? This way, since Class#name returns the full 'path', 
including modules, you can then do something like:

name=h[:class]
name.split('::').inject(Object){|res, c| c.const_get(c)}.new h

(taken from the solutions of the ruby quiz 133 
(http://www.rubyquiz.com/quiz113.html))

I hope this helps

Stefano