From: Kirk Haines Date: 2006-06-03T07:22:22+09:00 Subject: Re: Adding an Increment Operator? On Friday 02 June 2006 4:02 pm, Dave Howell wrote: > Although I will admit to being rather surprised that the answer to the > question > How do I do this? > whatever = 1 > whatever.increment > p whatever > => 2 > is apparently "You Can't." Why is it surprising? And you almost can, sort of, by relying on the syntactic similarity between a method call and a variable reference and using a simple counter object. self.whatever = 1 whatever.increment p whatever => 2 That, you can do. ----- class IncObj def initialize(val) @me = @val.to_i end def to_s @me.to_s end def to_i @me end def increment @me += 1 end end def whatever; @whatever; end def whatever=(val); @whatever = IncObj.new(val); end self.whatever = 1 whatever.increment p whatever ----- Kirk Haines