2006/5/28, Jeff Rose <rosejn / gmail.com>:
> This spawned from a conversation with a friend who was working in C++ on
> a system that is all reference count based.  He asked how it would work
> in ruby, and I didn't know.

You don't need that in Ruby.  GC will automatically take care of
removing objects that are not reachable any more.  For some form of
cache that may release data occasionally you can use WeakReference (an
object reachable through WeakReferences only may be removed).  Then
there are finalizers which can be used to execute some action if an
object is removed, but beware they are different than Java finalizers.
Also there are usually better methods of doing cleanup. The most used
idiom for this in Ruby are blocks and use the keyword "ensure" (see
File.open() for a typical example).  If you do want to do this
yourself, the idiom goes like this (silly example):

def work
  ar = Array.new( 10, 20 )
  begin
    return yield( ar )
  ensure
    # silly cleanup
    ar.clear
  end
end

irb(main):021:0* work {|x| x.inject(0) {|s,y|s+y}}
=> 200

HTH

Kind regards

robert

-- 
Have a look: http://www.flickr.com/photos/fussel-foto/