From: Charles Hixson Date: 2004-09-10T02:00:07+09:00 Subject: Re: Problem with ensuring consistency .. Finalization?? Austin Ziegler wrote: >On Thu, 9 Sep 2004 16:38:07 +0900, Charles Hixson > wrote: > > >>I want to have a class which occasionally updates a file, but I want to >>ensure that it always flushes it's data to the file before the program >>quits. >> >>The class is called Words >> >>What's the best approach to take here? I could just continually run a >>flush cycle, but that seems an awful waste of resources, I'd rather >>batch the updates, and only flush occasionally. >> >>class Words >> >> def initialize () >> @@words = Hash.new >> @@words.default = nil >> @@wtable = WordTable.instance #<<<================= >> @@maxWord = @@wtable.maxWord >> end # initialize >>.... >>end # Words >> >>WordTable is a singleton class, and it's the one that does the actual >>writing, but it needs to get the data to do the updates from Words. >> >>I'm considering: >> def initialize () >> ... >> ObjectSpace.define_finalizer(@@words, proc { flush }) >> >>But I don't know how to evaluate whether it's a good idea or not...or >>even how to tell afterwards (presuming it doesn't throw a "compile-time" >>error). >> >> > >Perhaps an END {} block? > >-austin > > That looks like a good approach (well, the best suggested). I'd rather have it tied into the class, so that the class could be moved from application to application, but if I can't, I can't. Having it a part of the containing file is certainly a "next best" viable approach. (I didn't even remember that END blocks existed, though I know I've read that page before!) Thanks loads.