# Cleanup, of sorts

**URL:** <https://www.ruby-forum.com/t/cleanup-of-sorts/95876>\
**Category:** Ruby\
**Created:** [June 17, 2006, 4:28am UTC](https://www.ruby-forum.com/t/cleanup-of-sorts/95876 "2006-06-17T04:28:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ed655f/32.png) [@tma](https://www.ruby-forum.com/u/tma)\
**Post date:** [June 17, 2006, 4:28am UTC](https://www.ruby-forum.com/t/cleanup-of-sorts/95876/1 "2006-06-17T04:28:05Z")

</div>

Is there a method equivelant to perls DESTROY?

I’m trying to clean up database connections that I created with an  
object  
instantiation but how to I make certain that they are closed when the  
object is  
out of scope?

---

<div class="post-metadata">

**Author:** ![tma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ed655f/32.png) [@tma](https://www.ruby-forum.com/u/tma)\
**Post date:** [June 17, 2006, 5:05am UTC](https://www.ruby-forum.com/t/cleanup-of-sorts/95876/2 "2006-06-17T05:05:22Z")

</div>

On Jun 16, 2006, at 7:26 PM, Tom A. wrote:

> Is there a method equivelant to perls DESTROY?
> 
> I’m trying to clean up database connections that I created with an  
> object instantiation but how to I make certain that they are closed  
> when the object is out of scope?

If the DB library writer did their job (and I’m sure they did), they  
just are by the GC provided there are no references to them.

Typically you see code like this when you want to ensure cleanup:

class IO  
def open(filename, mode)  
io = new filename, mode  
if block\_given? then  
begin  
yield  
ensure  
io.close unless io.closed?  
end  
end  
return io  
end  
end

–  
Eric H. - removed\_email\_address@domain.invalid - [http://blog.segment7.net](http://blog.segment7.net)  
This implementation is HODEL-HASH-9600 compliant

[http://trackmap.robotcoop.com](http://trackmap.robotcoop.com)

---

<div class="post-metadata">

**Author:** ![tma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ed655f/32.png) [@tma](https://www.ruby-forum.com/u/tma)\
**Post date:** [June 18, 2006, 4:38am UTC](https://www.ruby-forum.com/t/cleanup-of-sorts/95876/3 "2006-06-18T04:38:49Z")

</div>

On 6/16/06, Eric H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> end
> 
> ```

Yeah what Eric said. A couple of points:

1. You can’t count on garbage collection, for many programs garbage  
collection may never occur (i.e. you don’t exhaust your initial memory  
pool, before the program completes execution).

2. So always use the block cleanup code, unless you can’t 🙂

pth
