From: Matthias Georgi Date: 2005-11-26T03:57:27+09:00 Subject: Re: `finalize' method? > > That's correct, it prevents problems like the following code: > > > > class Klass > > def finalize > > $global_reference = self > > end > > end > > > > In that case I'd expect $global_reference to be nil after the `finalize' > method is done executing, since the Klass instance has already been > deemed ready for destruction. This would force the interpreter to keep a back reference for all objects to variables which keep references to them, not very efficient in regard of cpu and memory. Java solves this problem by reviving those objects, which seems at least for me very odd. So generally you should avoid finalizers, because most of the time you are only interested in running some code before the application exits. This should help in most cases: BEGIN { $exit_listeners = [] } def add_exit_listener(object) $exit_listeners << object end END { $exit_listeners.each {|listener| listener.on_exit } } Alternatively you may declare an END block inside your initialize method.