# Opposite of initialize

**URL:** <https://www.ruby-forum.com/t/opposite-of-initialize/166705>\
**Category:** Ruby\
**Created:** [April 28, 2009, 6:45pm UTC](https://www.ruby-forum.com/t/opposite-of-initialize/166705 "2009-04-28T18:45:36Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 28, 2009, 6:45pm UTC](https://www.ruby-forum.com/t/opposite-of-initialize/166705/1 "2009-04-28T18:45:36Z")

</div>

Hi,

I’m looking for the opposite function of initialize() as I need to  
call something before an object is “destroyed”.

Thanks  
Peter

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 28, 2009, 6:53pm UTC](https://www.ruby-forum.com/t/opposite-of-initialize/166705/2 "2009-04-28T18:53:48Z")

</div>

2009/4/28 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> Hi,
> 
> I’m looking for the opposite function of initialize() as I need to  
> call something before an object is “destroyed”.

[http://ruby-doc.org/core/classes/ObjectSpace.html#M006791](http://ruby-doc.org/core/classes/ObjectSpace.html#M006791)

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 28, 2009, 6:56pm UTC](https://www.ruby-forum.com/t/opposite-of-initialize/166705/3 "2009-04-28T18:56:45Z")

</div>

On 28 Apr 2009, at 17:45, removed\_email\_address@domain.invalid 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.

Ellie

## Eleanor McHugh Games With Brains [http://slides.games-with-brains.net](http://slides.games-with-brains.net)

raise ArgumentError unless @reality.responds\_to? :reason

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 28, 2009, 7:11pm UTC](https://www.ruby-forum.com/t/opposite-of-initialize/166705/4 "2009-04-28T19:11:26Z")

</div>

On 28.04.2009 18:55, Eleanor McHugh wrote:

> On 28 Apr 2009, at 17:45, removed\_email\_address@domain.invalid 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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 28, 2009, 7:29pm UTC](https://www.ruby-forum.com/t/opposite-of-initialize/166705/5 "2009-04-28T19:29:00Z")

</div>

On Tue, Apr 28, 2009 at 10:10 AM, Robert K.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

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.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 28, 2009, 7:06pm UTC](https://www.ruby-forum.com/t/opposite-of-initialize/166705/6 "2009-04-28T19:06:08Z")

</div>

Thanks. Exacalty what I need. Haven’t seen it.

On Apr 28, 5:55 pm, Eleanor McHugh [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 29, 2009, 1:42am UTC](https://www.ruby-forum.com/t/opposite-of-initialize/166705/7 "2009-04-29T01:42:09Z")

</div>

Hassan S. wrote:

> On Tue, Apr 28, 2009 at 10:10 AM, Robert K.  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

Not only that:

1. “it goes out of scope and there are no more references to the object  
so the object is ready to be garbage collected”

and

1. “the object is actually garbage collected and destroyed”

are two different things. The actual destruction may happen only after  
your program ends.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 28, 2009, 11:51pm UTC](https://www.ruby-forum.com/t/opposite-of-initialize/166705/8 "2009-04-28T23:51:01Z")

</div>

On 28.04.2009 19:28, Hassan S. wrote:

> On Tue, Apr 28, 2009 at 10:10 AM, Robert K.  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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](http://blog.rubybestpractices.com/posts/rklemme/002_Writing_Block_Methods.html)

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 29, 2009, 7:13pm UTC](https://www.ruby-forum.com/t/opposite-of-initialize/166705/9 "2009-04-29T19:13:57Z")

</div>

On Tue, Apr 28, 2009 at 7:42 PM, 7stud – [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > things.
> 
> are two different things. The actual destruction may happen only after  
> your program ends.

In every GCd language I’ve used, including Smalltalk, Java and Ruby,  
finalization has always turned out to be an unreliable mechanism for  
doing resource cleanup.

The role of the GC is actually to make sure that objects don’t get  
freed prematurely, a GC’s primary job is to prevent ‘dangling’ pointer  
problems. Reclaiming memory is a secondary goal. So there’s no  
guarantee that an object will be freed as soon as it’s no longer  
reference-able. And as you point out, objects might only be reclaimed  
by the OS after the program exits.

Over the past 25 years or so, I’ve come to regard finalization as an  
“attractive nuisance.”

–  
Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)
