From: MikkelFJ Date: 2001-11-29T07:13:31+09:00 Subject: [ruby-talk:26844] Re: Ref Counting (was KDE or GNOME curiosity question...) "Paul Brannan" wrote in message news:Pine.LNX.4.40.0111281609220.2741-100000@zaphod.atdesk.com... > On Thu, 29 Nov 2001, mark hahn wrote: > > > I'm thinking now that using scope is the only good way to get quick > > finalization, with the fopen/block trick being the best example. > > The problem with scoping is that it doesn't work if you need to share the > object among many different other objects. > > I've never personally run into this problem, but I know that it can come > up. Just recently, in [ruby-talk:25477], there was talk of wanting to > close a database connection automatically. To summarize, the author > wanted to use a finalizer, but he was (fortunately) able to use scoping > instead. Matz said he was in favor of explicit disconnection from the > database. However, imo, explicit disconnection could be error-prone, but > it's probably the only reliable way to do this in Ruby for the cases where > a block will not suffice. But you can combine scoping with subscribe unsubscribe as mentioned in other mail class ResourceHandle def initialize(resource) @count = 0 @resource = resource end def subscribe() @count = @count + 1 end def unsubscribe() @count = @count - 1 if @count = 0 @resource.close end end end Usercode with block scope magic left as an exercise. MikkelFJ