Kevin Smith wrote:

> tonys / myspleenklug.on.ca wrote:
> 
> 
>> i guess i can live with 26 lines initializing each planet...but i think that
> 
>> really needs to be addressed in ruby. i'm envisioning a multiplayer
> 
>> server/client version where i won't have just 26 planets...more like 1000.
> 
>> 
> 
>> is there NO way to initialize a large number of objects in ruby?
> 
> 
> 
> Dave's answer to this was right on the money. And 
> 
> one of the big benefits of the array approach is 
> 
> that you can add and remove planets at will, 
> 
> while the game is running. Perhaps that's not a 
> 
> big deal for planets, but it sure is for ships.
> 
> 
> 
> Perhaps if you give us a short example in some 
> 
> other language of what you're trying to do, we 
> 
> could provide a Ruby way to do it in roughly the 
> 
> same way.
> 
> 
> 
> Kevin
I don't really understand the question either, but 
here are three additional answers to things that you 
might have been asking about:


If you are thinking of a sparse array with default 
values, then you could create a SparseArray class 
with a default size, a re-sizer method, and [] and 
[]= methods that would retrieve default values (and 
delete values when they were set back to the default 
value.  Then only the non-default values would need 
to be stored.

If you wanted a kind of array that only had one 
entry per value, you could write a UniqValArray 
class that was backed by a hash where the values 
were really stored.  Then the array could consist of 
hash values, etc.  (To do this it looks to me like 
you might end up re-writing a lot of the hash class, 
so that all you would need to store in your array 
would be a hash-key, and you could do direct 
hash-key based retrieval from the hash.  I suppose 
that you might define a key-string that would be 
used as a hash-retrieval, and store the key in each 
cell of the array, but that seems to defeat all 
purpose in this.  The other ways that I can thing of 
doing this end up being too slow (unless you have a 
REALLY LARGE collection, when you might want to use 
a B+ Tree backing collection).

Actually, the AVL tree and B+ tree approaches are 
the two remaining choices.  These aren't typical 
with Ruby, as they are really only appropriate for 
rather large collections.  They are, however, 
commonly used in databases, and I've used them a 
couple of times in compiled code.  (Once in PL/1 and 
once in C.)  I usually don't create a custom 
version, however.  Try linking your code against a 
database like SleepyCat if that's what your want. 
It will probably be easier and more effective.

-- 
Charles Hixson

Copy software legally, the GNU way!
Use GNU software, and legally make and share copies 
of software.
See http://www.gnu.org
     http://www.redhat.com
     http://www.linux-mandrake.com
     http://www.calderasystems.com/
     http://www.linuxapps.com/