> How hard would it be to have an option to use reference counting in a
> particular class?  This class would be handled normally by the garbage
> collector, but instances of this class would have a method called
> immediately when the last reference is gone (before the finalizer).

I've been wondering the same thing.  A few of problems with reference
counting:

  1) Circular references will cause the object to stay around indefinitely.
  2) If the object is referenced from an anonymous function, it will stay
     around indefinitely (or, at least, until the anonymous function is
     garbage-collected, in which case you have suddenly turned your
     reference counting GC into a mark/sweep GC).
  3) More generally, any object that holds a reference to a
     reference-counted object must itself be reference-counted; a
     mark/sweep object with a finalizer will render the reference-counting
     ineffective.

That said, it is possible to write a reference-count class in Ruby, but it
will have limitations.  I've hacked a pure Ruby implemetation together,
but it is not pretty, and the object itself never goes away (but the
finalize method does get properly called).  Perhaps someone else already
has a good implementation, since this topic has come up on the ML before?

Paul