From: Gavin Kistner Date: 2005-10-02T03:10:37+09:00 Subject: Creating a Class factory As noted in another thread, I'm looking at ways of creating a Struct- like class; one that is a factory for creating classes. I did it one way by using Struct itself, but I'm not wild about it. I'd like to write my own, that sets true instance variables instead of struct's members thing. Is Struct using C magic during its initialize to cause "Struct.new" to return class objects, or is it just doing the equivalent of the following: class Foo def self.new klass = Class.new( self ) # setup klass here klass end end Is there a better way to make a class that creates 'instances' which are also classes? Erps, except that the above doesn't work, because if you ask the new class to inherit from the factory, then 'new' is overridden and doesn't call initialize. For example: class KeywordClass def self.new( property_hash ) klass = Class.new( self ) code = <<-ENDCODE attr_accessor #{property_hash.keys.map{|k| k.to_s.intern.inspect}.join(', ')} def initialize( values={} ) #{property_hash.map{ |prop,default| "@#{prop} = values[#{prop.inspect}] || # {default.inspect}" }.join("\n")} end ENDCODE klass.class_eval code klass end def foo "whoa" end end People = KeywordClass.new :cat=>true puts People.new.cat #=> ArgumentError: wrong number of arguments (0 for 1)