tags:

views:

293

answers:

4
+8  A: 

Ruby has no pre/post increment/decrement operator. For instance, x++ or x-- will fail to parse. More importantly, ++x or --x will do nothing! In fact, they behave as multiple unary prefix operators: -x == ---x == -----x == ...... To increment a number, simply write x += 1.

Taken from Things That Newcomers to Ruby Should Know.

That explains it better than I ever could.

EDIT: and the reason from the author himself:

  1. ++ and -- are NOT reserved operator in Ruby.
  2. C's increment/decrement operators are in fact hidden assignment. They affect variables, not objects. You cannot accomplish assignment via method. Ruby uses +=/-= operator instead.
  3. Self cannot be a target of assignment. In addition, altering the value of integer 1 might cause severe confusion throughout the program.
Dave
A: 

Use

i += 1

Why? Because it's not C/++/#/PHP/Java! It's a different language, so deal with different types of (non-existing) operators. There is no "real" reason, there is just a reason.

And that reason is: Because! I don't mean to be rude, but dammit, it's a different language, and different languages tend to differ in terms of operators and syntax (You know, because they're different)...

ApoY2k
Keep cool .....
Cedric H.
"Because it's a different language" is not an answer. He's asking if there's a good _design_ reason the language doesn't have this common, useful, feature. Other answers have explained why it doesn't really fit Ruby, which is _not_ because the creator just wanted to make things difficult.
John
@John: the OP asked specifically for "a REAL reason". That's basically troll-speak for "All your reasons are stupid." The technical and language design reasons for why there is no post-increment operator in Ruby, are already extensively documented on StackOverflow, they have been discussed to death literally hundreds of times on the ruby-talk, ruby-core and ruby-dev mailinglists, they are thoroughly explained in pretty much every single Ruby book, tutorial, manual, course and blog ever written. Matz himself has explained it multiple times, and continues to explain it about once a month, ...
Jörg W Mittag
... every time the question gets brought up again and again and again and over again. If the OP doesn't consider those reasons "REAL" enough, then there's really no point in giving any other answer, other than: "Just because".
Jörg W Mittag
"Because!"? What do you mean? The exact reason the OP is looking for is stated in the "Things That Newcomers to Ruby Should Know" article. I can understand that this topic has been discussed to death in the Ruby community but dont make it look even more troll-ish to outsiders.
mizipzor
@mizipzor: The OP very specifically used the wording "REAL reason". IN SCREAMING CAPS. That is *pretty* strong wording, since it basically implies that all the reasons he was given so far, are somehow "UNREAL". He also chose to re-ask an *already* answered question on StackOverflow, which implies that he considers all the reasons given there as "UNREAL", otherwise he wouldn't have needed to re-ask the question. So, basically, the OP is insulting everybody who contributed to the other SO question(s) on this topic by telling them their reasons are "not REAL", and he refuses to clarify what ...
Jörg W Mittag
... exactly he means by "REAL". Also, let's not forget that a post-increment operator is a very uncommon language feature that only exists in a small handful of languages. Unlike first-class functions (or procedures), for example, which are very common. If you ask *any* language designer, why they didn't include any of the gazillion niche features that exist only in a handful of languages, they'd probably tell you the same: "Because." You simply can't put every single feature into a language, and so you need to focus on the important ones.
Jörg W Mittag
+4  A: 

From a posting by Matz:

(1) ++ and -- are NOT reserved operator in Ruby.

(2) C's increment/decrement operators are in fact hidden assignment. They affect variables, not objects. You cannot accomplish assignment via method. Ruby uses +=/-= operator instead.

(3) self cannot be a target of assignment. In addition, altering the value of integer 1 might cause severe confusion throughout the program.

                      matz.
mikej
Surely they could be methods on the variable object…?
Donal Fellows
@Donal if you mean, could they be retrospectively be added to the language without breaking things then I don't think they could. `x = a++b` parses as `x = a + (+b)` so if we had just `x++` then Ruby expects another operand. This is why you tend to get syntax errors if you try to use `++` at the moment as Ruby takes part of the next statement as the operand. `+=` is implemented as call `+` on the receiver passing the RHS as a parameter and then assign the value returned by `+` to the variable.
mikej
@Donal Fellows: variables aren't objects in Ruby.
Jörg W Mittag
I know they can't be done syntactically without breaking backward compatibility, and I think that Matz is right not to do them, but the argument that increment can't be done because of hidden assignment is *in general* wrong. If variables are objects then increment is naturally a method on the class of variables. Don't know if such a concept sits well with Ruby's model of the world. :-)
Donal Fellows
@Donal Fellows: But variables aren't objects in Ruby, that's why post-increment can't be implemented as a method on variable objects.
Jörg W Mittag
@Jörg: I'm not claiming that they are. Just pointing out that if they were, then you'd be able to do cool stuff. (I have no idea how that would work syntactically; I was thinking purely at the high-level semantics level. Smug Smalltalk Weenies would probably laugh at this discussion.)
Donal Fellows
A: 

I don't think that notation is available because--unlike say PHP or C--everything in Ruby is an object.

Sure you could use $var=0; $var++ in PHP, but that's because it's a variable and not an object. Therefore, $var = new stdClass(); $var++ would probably throw an error.

I'm not a Ruby or RoR programmer, so I'm sure someone can verify the above or rectify it if it's inaccurate.

Martin Bean