From: "Brian Schröder" Date: 2005-03-03T23:34:35+09:00 Subject: Re: post inc problem > > Is the "++" syntax used for some other operation, thus precluding > > its use for a synonym for increment. And wouldn't the right thing > > for ++ be to increment to the next number? > > This comes up every so often, and as much as it has to do with the > problem of mutating a fixed value (e.g., 5++), it has much more to > do with the understanding of Ruby variables and the true nature of > object orientation in Ruby, as well as operator overriding. > > At least, IMO. > > There's two ways to implement ++. The first way is to implement it > as an operator, like #+ and #*. Because it is a unary operator, it > would probably be something like: > > def @++ > # implementation > end > > OK. That's fine. Maybe I didn't understand you, but how would such an implementation look like? The problem as I see it, is that we are sending messages to the object and not to the variable. So we can't increment the variable, because we don't know it. z = y = x = 5 => 5 x.++ => 6 Now which value does z have, and which value does x have? regards, Brian > That's probably the better way, but then you've got > the question of -- how do you distinguish between prefix and > postfix? So now, you need *two* operators instead of one -- because > you know that people will use both (they have different meanings). > But you have to define both if you want one. > > Ouch. > > Okay, the other way is as syntax sugar. Define "a++" such that it is > the same as "a += 1". But what if a is a String? Your program blows > up. So we're back to option 1. > > -austin -- Brian Schr�der http://ruby.brian-schroeder.de/