From: Phlip Date: 2001-11-23T05:16:05+09:00 Subject: [ruby-talk:26175] 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? When I applied the first big Refactor (yippee!) to my Flea project, "Replace Monster Switch-Case with Polymorphism," I just typed in code that passes the class name as a reference, and then calls 'new' on it. I was incompletely aware of the : colon operator at the time, but nothing stopped me: class ActLeft < Action # ... end # ... def _absorbAction act, q = nil action = act.new (q) @actions << action return self end def left q = nil; return _absorbAction (ActLeft , q); end def longer q = nil; return _absorbAction (ActLonger , q); end def move q = nil; return _absorbAction (ActMove , q); end That's all; the reference 'act' links the class 'ActLeft', so 'act.new' calls 'ActLeft.new'. Now what was the problem again? (And why are many of the other answers to this question so exotic??) (BTW I would appreciate design suggestions about flea's object model, now that I have a long weekend with lots of carbohydrate highs to work in ;-) http://flea.sourceforge.net -- Phlip http://www.greencheese.org/HatTrick