From: Albert Wagner Date: 2002-01-10T12:41:26+09:00 Subject: Re: How to override subclass#new Nevermind. I've solved my own problem. class A def A.new(a, *args) end end class B < A @@b = nil @@c = nil def B.new(a, b, c) @@b = b @@c = c x = 1 y = 2 z = 3 super(a, *args) end def initialize(a, *args) @a = a @b = @@b @c = @@c end end B.new(a,b,c) #<==preferred method of creating a B instance It still feels like a kludge, but it works. For those who questioned why and offered no how: you learn by doing; There is no better reason. But in fact, it wasn't "new" that I was adding behaviour to, but rather "create" and "open", aliases for "new". "A" is a c code extension of a file manager(BDB). x, y, z, and etc are a variable number of attributes that determine, among other things, whether a new file is to be created or an existing file is to be opened for update. I wanted to subclass "A" so that I could embed code to convert a Ruby Struct to delimited string format for persistent storage in the database. (Unlike relational DBs, Berkely DB does not keep track of fields; a record is just a binary string of arbitrary length) I wanted this "catalog" type info to be contained in the file object and entered only once. I also disliked the ugliness of a long string of file attributes cluttering up my main code. Additionally, with eveything pertaining to a file properly contained in the file object, I was able to override the [] and []= methods of the "A" file object, so that I could interject code to format/unformat records cleanly. I can now do code like this for the file catalog: if File.exists?(@catName + '.bdb') @cat = Catalog.open(@catName) else @cat = Catalog.create(@catName) end @cat[fileName] = fileFieldListAndOtherFileInfo fileFieldListAndOtherFileInfo = @cat[filename] And identical functionality for the data files. This is much cleaner. And it greatly pleases me. -- Quantum Mechanics: The dreams stuff is made of