"Simon Strandgaard" <neoneye / adslhome.dk> schrieb im Newsbeitrag
news:pan.2004.02.25.15.23.19.681692 / adslhome.dk...
> On Wed, 25 Feb 2004 16:12:16 +0100, Robert Klemme wrote:
> [snip]
> > What do others think?  Did I overlook something?
> >
>
> It would be really nice to be able to overload '+=', so that #close can
be
> invoked correct.

Your problem is, that += silently discards an instance without invoking
some kind of "destructor" and without the chance to detect this situation.
Is that correctly analyzed?

You could indeed deal with that when overriding of + and += was swapped:
in that case you'd only have to override #dup to increment the counter.

> server> ruby a.rb
> 0
> 1
> server> expand -t2 a.rb
> class A
>   @@count = 0
>   def initialize(value=nil)
>     @value = value || 0
>     @@count += 1
>   end
>   attr_reader :value
>   def close
>     @@count -= 1
>   end
>   def A.count
>     @@count
>   end
>   def +(value)
>     A.new(@value + value)
>   end
> end
>
> p A.count
> a = A.new
> a += 5
> a.close
> p A.count
> server>
>
> --
> Simon Strandgaard

Kind regards

    robert