OK, that's good enough for me. I had assumed that
Struct.new("Foo","a","b","c") was more or less the same as
class Struct
class Foo
attr_accessor :a,:b,:c
end
end
So more general question, what is the recommended way to generate a class
with a dynamic name? I really want to avoid the string version of eval:
klassname="Foo"
eval "class #{klassname} ... "
because this seems like a last resort 
Just as background: I am generating classes which represent rows in a table;
when table X is read in, the accessors are created automatically based on
the column names. I can then read in different tables, and I get a fresh
class for the rows of each one, all with the right attributes.
This is convenient:
a = Table.new(dbh,'select * from foo','id') # id = primary key field
puts a[17].name
A Struct is fine here: @klass = Struct.new("Foo","id","name","address"...)
is generated automatically, and each row read in is generates a new object
of this class.
But then I want to add additional information when postprocessing the rows;
for example add a new attribute called 'children' which is a hash of objrefs
to rows in another table. That's what these dynamically-added attr_accessors
are for.
If Struct is not the right thing for this case, then maybe I would be
better storing each row as an object consisting of:
* array [of values]
* objref [to object which describes the column names]
and then using method_missing to select a column? I doubt it would be as
efficient as Struct though.
Regards,
Brian.
···
On Thu, Feb 20, 2003 at 01:26:42AM +0900, Yukihiro Matsumoto wrote:
>I want a general method to add a member to the class dynamically, so if
>there were
> Struct#add_member
>I would happily use it.
Ah, probably you have misunderstood Ruby's Struct. It is a fixed
array with symbolic index. Members should *not* be added dynamically.
Use Objects and attributes instead for dynamic slots.