From: Kent Dahl Date: 2001-11-23T00:55:45+09:00 Subject: [ruby-talk:26146] Re: Passing class names to constructors. Hugh Sasse Staff Elec Eng wrote: > > If I want to create a variable number of objects, all of > subclasses of one class, then how do I do that? > Eg in my knots example I might want > > class TangledRope > def initialize(knotclass = :Knot) > mess = Array.new() > (10*rand()).times do { something = Module.const_get( knotclass ) mess.push( something.new() ) > } > end > > so how do I turn the symbol into the class, so I can do things with it? > I don't like the obvious way: Deliver Us From Eval! :-) Module.const_get( aSymbol ) # is much nicer that eval -- <[ Kent Dahl ]>================<[ http://www.stud.ntnu.no/~kentda/ ]> )____(stud.techn.;ind.�k.data)||(softwareDeveloper.at(Trustix))_( /"Opinions expressed are mine and not those of my Employer, "\ ( "the University, my girlfriend, stray cats, banana fruitflies, " ) \"nor the frontal lobe of my left cerebral hemisphere. "/