From: Robert Klemme Date: 2005-05-03T15:04:30+09:00 Subject: Re: collections with values of fixed classes/lengths "Eric Mahurin" schrieb im Newsbeitrag news:20050502203538.19806.qmail@web41106.mail.yahoo.com... >> > That would be great. What do you mean by "fixed size >> type"? >> > Does that mean you couldn't use this class to make a >> > homogeneous array of strings, arrays, or hashes? >> >> Exactly. Because for that to work you need info about each >> instance >> stored there. And then you have Array. > > But, you still wouldn't have to store the class info for each > element. I'm not sure of the underlying Ruby object data > structure, but at a minimum, it should get rid of a pointer (to > the class) per element. I think you'd have to implement this > in C to do it right. I don't think it's worth the effort. That must be a very special case where you need huge collections of inhomogenous data where you want to squeeze out every byte. I can't think of an application of this. Plus, it'll be error prone and very inefficient. > Thanks for the implementation. Another option for implementing > this in Ruby may be to use Marshal dump/load and remove/prepend > the class information. You would at least be able to remove > the class of the top-level object. > > I still like having to specify a default object (instead of > just a class) for specifying what types of objects are in the > collection because: > > * when the collection expands with empty elements you need to > put something there. nil won't work because it is a different > class (NilClass). You need a default object to put in > otherwise what comes out is undefined (may give garbage or > exceptions). Just add a method to the traits that returns this object (or can create a new object). > * you can handle more general classes because you have access > to the "instance_variables" (or "members" for Struct), etc. > You can also contemplate using Marshal - for completely > fixed-length objects. What would you gain by this? Every instance of a single class can have a complete different set of instance variables. > * you can handle deep objects by using the default object as a > template for how deep the homogeneity runs. This sounds too theoretical for me. > * this default/template object can also provide a mechanism for > specifying fixed-length strings/array/integers. I'm sorry, I don't get you here. What do you mean by "specifying"? Regards robert