From: Simon Strandgaard Date: 2003-12-01T02:22:09+09:00 Subject: Re: selfassignment and close On Fri, 28 Nov 2003 14:54:59 -0700, Ara.T.Howard wrote: > On Fri, 28 Nov 2003, Simon Strandgaard wrote: >> I would like to overload the '+=' operator. But it doesn't seems to be >> possible? If it isn't possible, then how to use the '+' operator and at >> the same time get the instance #closed correctly ? [snip code] > > if you rely on ObjectSpace.define_finalizer you don't really know when the > call will be made, but perhaps something using it would be better... this > would cause an apparent 'leak' in long running programs. the general idea - > track objects within the class itself and make sure to free them - should be > workable for you though. I had something similar, a WrapperIterator which kept track of all clones. When the operation were completed, I could then sweep them. But I don't consider it as a 'real' solution :-) It has to be solved _correct_, before im satisfied. -- Simon Strandgaard server> expand -t2 wrapper_close.rb require 'iterator' # http://raa.ruby-lang.org/list.rhtml?name=iterator class Scanner def initialize(iterator) @iterator = iterator end def execute a = @iterator.clone b = @iterator.clone c = @iterator.clone d = @iterator.clone end end # keep track of how many instances we have class CountingIterator < Iterator::Collection @@count = 0 def clone @@count += 1 super() end def self.count; @@count end def close @@count -= 1 super() end end class WrapperIterator < Iterator::Base def initialize(iterator, stack=nil) @iterator = iterator @stack = stack || [] end def clone i = @iterator.clone @stack << i WrapperIterator.new(@iterator, @stack) end def close @stack.map{|i| i.close; nil} end end ary = (0..19).to_a iterator = CountingIterator.new(ary) #wrap_iterator = iterator wrap_iterator = WrapperIterator.new(iterator) s = Scanner.new(wrap_iterator) s.execute p CountingIterator.count wrap_iterator.close # ensure all iterators gets closed p CountingIterator.count server> ruby wrapper_close.rb 4 0 server>