From: Robert Klemme Date: 2009-04-29T06:50:07+09:00 Subject: Re: Opposite of initialize On 28.04.2009 19:28, Hassan Schroeder wrote: > On Tue, Apr 28, 2009 at 10:10 AM, Robert Klemme > wrote: > >>>> I'm looking for the opposite function of initialize() as I need to >>>> call something before an object is "destroyed". > >> And the finalizer is called *after* the object has gone! > > So "before" it's destroyed and "after" it's destroyed are two different > things. Absolutely! > ObjectSpace#finalizer wouldn't seem to fit the original request, if the > OP is looking to actually *do* anything with the object (retrieve some > attribute or whatever). > > Curious whether there's something else that would enable that case. You can use the mechanism I wrote about recently[1] which is not exactly the same as a finalizer but can ensure timely resource deallocation. In a way it's even better because you get immediate cleanup when the resource is not used any more and not delayed cleanup (i.e. when the garbage collector decides that he wants to collect the instance). Kind regards robert [1] http://blog.rubybestpractices.com/posts/rklemme/002_Writing_Block_Methods.html