From: Kevin Smith Date: 2001-11-23T01:02:09+09:00 Subject: [ruby-talk:26148] 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 { > mess.push(knotclass.something.new()) > } > end Here's my attempt. Kevin --- class Upper def initialize(text) @text = text.upcase end end class Lower def initialize(text) @text = text.downcase end end def factory(text, klass=Upper) return klass.new(text) end a = factory('Hello') b = factory('Hello', Lower) p a.inspect p b.inspect __________________________________________________ Do You Yahoo!? Yahoo! GeoCities - quick and easy web site hosting, just $8.95/month. http://geocities.yahoo.com/ps/info1