From: Paul Brannan Date: 2002-05-29T01:56:22+09:00 Subject: Re: Stymied by Ruby's garbage collector On Tue, May 28, 2002 at 10:34:19PM +0900, Nat Pryce wrote: > I've never missed the lack of destructors in languages other than C++. > Perhaps its because in C++, it is idiomatic to use the destructor as part of > the object's protocol, while in OO languages with GC, as you say, one > implements those parts of the protocol as explicit method calls. I usually > only use finalisers to ensure safety in the presence of unexpected > exceptions, rather than to clean up objects when no longer needed, as C++ C++ destructors are also used to clean up when an exception is thrown. I tend to use ensure blocks to do this in Ruby, because with finalizers, you have to make sure you aren't somehow accidentally holding a reference to the object (via a proc or continuation or something). > destructors are. In my experience it's much easier to write exception-safe > code in Ruby than it is in C++. Swings and roundabouts... If your objects hold only pointers and POD types, then writing exception-safe code in C++ becomes much easier (try writing an assignment operator for an object that doesn't holds non-POD data and you will see what I mean). In Ruby, objects can't hold anything other than object references (pointers), so the problem becomes much simpler. It's not a matter of idioms, but of features. Paul