From: Nat Pryce Date: 2001-11-23T01:09:34+09:00 Subject: [ruby-talk:26151] Re: Passing class names to constructors. Don't pass in a symbol, pass in a reference to the class object you want to use. The class object is a Factory. See http://www.rubygarden.org/ruby?AbstractFactoryPattern for more detail. E.g. class TangledRope def initialize( knotclass = Knot ) mess = Array.new() (10*rand()).times do mess.push(knotclass.new()) end end Cheers, Nat. ________________________________ Dr. Nathaniel Pryce B13media Ltd. 40-41 Whiskin St, London, EC1R 0BP, UK http://www.b13media.com XP Day, London, UK. 15th December 2001. http://xpday.xpdeveloper.com/ ----- Original Message ----- From: "Hugh Sasse Staff Elec Eng" To: "ruby-talk ML" Sent: Thursday, November 22, 2001 3:32 PM Subject: [ruby-talk:26141] Passing class names to constructors. > 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 { > mess.push(knotclass.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! :-) > > Hugh > > > >