From: Gabe Boyer Date: 2006-08-04T01:48:55+09:00 Subject: Re: Creating Classes at runtime ------=_Part_16148_11558857.1154623727611 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline On 8/3/06, Gabe Boyer wrote: > > On 8/3/06, David Stokar wrote: > > > > Hi everybody, > > this is my first post and allready a nifty question ;-) > > > > What I want: > > Create a NAMED class at runtime. Adding functions is clear. > > > > > > E.g. somthing like that: (ClassGenerator is an existing module) > > module ClassGenerator > > def self.generate_new_class ( Parent_Class_Name, Name ) > > def self.add_method( source ) > > end > > > > somewhere else: > > > > ClassGenerator::generate_new_class( Fixnum, MyInt ); > > ClassGenerator:: MyInt.add_method( %{ def special_one > > return 2 > > }% > > ) > > > > Thanks, > > David Stokar > > > > -- > > Posted via http://www.ruby-forum.com/ . > > > > > > Something like the following should accomplish what you want: > > module ClassGenerator > def self.generate_new_class(name, parent = Object) > const_set(name, Class.new(parent)) > end > end > > - Gabe > Sorry David, I should have added a usage example. 'name' should be a symbol: ClassGenerator.generate_new_class(:MyInt, Fixnum) - Gabe ------=_Part_16148_11558857.1154623727611--