On May 6, 2006, at 2:55 PM, Eli Bendersky wrote: > Robert Klemme wrote: >> 2006/5/6, Eli Bendersky <eliben / gmail.com>: >>> class Bar >>> Another is dynamic creation of new classes using Struct: >>> to do): >>> >>> >>> How can this be done ? >> >> You'll have to modify Struct to be able to create sub classes of >> arbitrary classes and not just Object. Btw, if feels a bit strange to >> have a nested class that is a sub class of the outer class. By that >> twist it contains itself - I think this is not a good idea. >> > > Sorry, I don't understand what you mean by "have a nested class > that is > a sub class of the outer class". > > Perhaps it's better for me to explain the motivation for what I'm > trying > to do. Simplifying: > > I have a flat-file full of records. The way records are stored can be > customized via a separate configuration file. I want to have a class > Database which is a container of Record classes. The Record is best > implemented as a Struct, IMHO, and I want to create the class > dynamically after reading the configuration (which says which fields > there are in a Record). I think it would be convenient for a Record to > be a subclass of Database, since there is no point having Records > without a Database. > Eh, I'm not sure you get the is-a relationship. Saying that a Record is a subclass of Database implies that anywhere you would use a Database you could use a Record instead. That doesn't really make sense. It would also seem to imply that a Record could contain many records. Instead of class Database ... end class Record < Database ... end I would suggest class Database def self.new(file_containing_info) fields = parse_field_format(file_containing_info) Database.const_set('Record", Struct.new(*fields)) super end def initialize(file) @records = [] read_each_record_in_file(file) do |record_data| record = Database::Record.new extract_record_data_into(record_data, record) @records << record end end end > -- > Posted via http://www.ruby-forum.com/. >