From: Austin Ziegler Date: 2004-02-26T01:05:44+09:00 Subject: Re: Operators +, += and = in Ruby 2 On Thu, 26 Feb 2004 00:29:46 +0900, Simon Strandgaard wrote: > 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. > 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 I don't see the problem here. p A.count # => 0 a = A.new p A.count # => 1 a += 5 p A.count # => 2 a.close p A.count # => 1 It's doing exactly what you told it to do here. I'm presuming that you want to do: def +=(value) @value += value end Ultimately, what you're doing with + in this case is trying to do a #become operation, which Matz has -- at least as far as I can see -- said he won't support. -austin -- austin ziegler * austin@halostatue.ca * Toronto, ON, Canada software designer * pragmatic programmer * 2004.02.25 * 11.00.12