From: David Alan Black Date: 2002-07-26T23:24:42+09:00 Subject: Re: dynamic object creation Hi -- On Fri, 26 Jul 2002, Daniel Berger wrote: > Hi all, > > I couldn't quite find the answer I was looking for on Google (though > it's probably there somewhere), so I'll pose the question here. > > I have a situation where the class and module names are identical. I > want to dynamically generate an object based on a string. > > m = "TestClass" > require "#{m}" > t = eval "#{m}.new" > > This works, but I'm wondering if there's a more "graceful" approach. #require just wants a string: irb(main):005:0> d = "parsedate" "parsedate" irb(main):006:0> require d true To go from string to constant, you can use Module#const_get: irb(main):007:0> s = "String" "String" irb(main):008:0> str = type.const_get(s).new("hi") "hi" David -- David Alan Black home: dblack@candle.superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav