Robert Klemme
4/28/2009 5:05:00 PM
On 28.04.2009 18:55, Eleanor McHugh wrote:
> On 28 Apr 2009, at 17:45, peter.meier998@gmail.com wrote:
>> I'm looking for the opposite function of initialize() as I need to
>> call something before an object is "destroyed".
>
> ObjectSpace#define_finalizer allows you to have a proc executed when
> an object is about to be destroyed. If I remember rightly this is when
> the object is actually garbage collected, which can be some time after
> all its references have been released.
And the finalizer is called *after* the object has gone! Peter, please
do also note that there are some subtle issues lurking. One important
thing to remember is that you must not create the finalizer in an
instance method (such as #initialize) because it will bind "self" and
thusly prevent GC of the instance.
Kind regards
robert