From: HarryO Date: 2002-01-13T05:46:45+09:00 Subject: Re: destructor in Ruby On Sun, 13 Jan 2002 06:59:57 +1100, C8 wrote: > Hi, > > I'm new in ruby. How can we code a destucter in Ruby ? I've the > following problem. > I've code a class client, one of his attribute is a session, I want to > destroy the object when I call a methode of my object which close the > session. How can I do that ? There are no destructors per se. Ruby does provide a facility to add a "finalizer" to an object, which will be called when the garbage collector gets around to cleaning up your object. You can force that to happen, to some extent, if you know when your object will become garbage, by explicitly asking the garbage collector to start. However, I think a nicer approach would be for your session object's constructor to accept a block, like File.initialize does. Ie, you can do ... File.new("somefile") do |file| # Do something with the open file end When the block completes, the file will be closed. I think you could do the same thing with your session class. I've probably simplified things a bit here, but I'm sure someone else will chime in and explain it more clearly. Hope that helps.