On Sat, Mar 09, 2002 at 05:12:49AM +0900, Ron Jeffries wrote: > I'd like to set the thing up so that no matter how badly things blow up "up > above", the file gets closed. I can see that I could do that with the block > trick like the one on File.open, but I'm not sure I can limit the usage of the > object that extremely. You can use a finalizer, but the GC won't necessarily be invoked at a convenient time (it might not get invoked until the program exits). You can also implement your own reference counting and require that the user explicitly obtain a reference to your object. When the reference count drops to zero, close the file. The ideal case, as you mentioned, is to pass a block to File.open. To ensure that the user uses your object correctly, you should write your own allocation function and disable new(): class Foo private_class_method :new public def initialize(filename) @f = File.open(filename) end def self.allocate(filename, &block) obj = self.type.new(filename) begin yield obj ensure obj.finalize end end private def finalize @f.close end end Hope this helps, Paul