"Charles Hixson" <charleshixsn / earthlink.net> schrieb im Newsbeitrag
news:41408EA1.9030204 / earthlink.net...

> What's being done here is updating a database (an Sqlite database
> actually).  So what I want to do is accumulate a bunch of changes, and
> then periodically add them either when things would be idle or when the
> number of changes starts to use too much ram.  But I don't want to loose
> them when the program terminates, and the class doesn't terminate itself
> (notice that the table is referred to, indirectly, via a class variable
> @@wtable).  Now the data accumulation happens in a class separate from
> the class that manipulates the database table, etc.
>
> The stream.sync approach doesn't seem to apply here at all.  (Note that
> I want to be flushing data in a Hash Table to the file...so I can't use
> any automatic file flushing.)

I don't know how big your hash will grow, but did you try to just marshal
the hash like this after every change you want to preserve.  Marshal is
quite fast, so it might be worth a try:

File.open("storage", "wb"){|io| Marshal.dump( hash, io )}
hash = File.open("storage", "b"){|io| Marshal.load(io)}

> The suggestion of the END block of the file is a plausible approach,
> which I had forgotten existed.  What I really want is a class finalizer,

As mentioned ruby seems to flush all open handles on exit.

> but lacking that I should be able to make the END block work, with a bit
> of redesign.  It will drastically decrease the portability of the class,
> but as each file can have it's own END block, it shouldn't decrease the
> portability of the file.

If you want to save for safety reasons (i.e. to avoid data loss on a crash
of the Ruby interpreter) you must flush after every write anyway.  So
there would be no need for END block or whatever other means.

Kind regards

    robert