tags:

views:

194

answers:

5

The pre/post increment/decrement operator (++ and --) are pretty standard programing language syntax (for procedural and object oriented languages, at least).

Why doesn't Ruby support them? I understand you could accomplish the same thing with += and -=, but it just seems oddly arbitrary to exclude something like that, especially since it's so concise and conventional.

example:

i = 0    #=> 0
i += 1   #=> 1
i        #=> 1
i++      #=> expect 2, but as far as I can tell, 
         #=> irb ignores the second + and waits for a second number to add to i

I understand FixNum is immutable, but if += can just instanciate a new FixNum and set it, why not do the same for ++?

Edit

Is consistency in assignments containing the = character the only reason for this, or am I missing something?

+2  A: 

One reason is that up to now every assignment operator (i.e. an operator which changes a variable) has a = in it. If you add ++ and --, that's no longer the case.

Another reason is that the behavior of ++ and -- often confuse people. Case in point: The return value of i++ in your example would actually be 1, not 2 (the new value of i would be 2, however).

sepp2k
More than any other reason so far, the rational that "all assignments have a `=` in them" seems to make sense. I can sort of respect that as a fierce adherence to consistency.
Andy_Vulhop
+6  A: 

Here is how Matz explains it in an old thread:

Hi,

In message "[ruby-talk:02706] X++?"
    on 00/05/10, Aleksi Niemelä <[email protected]> writes:

|I got an idea from http://www.pragprog.com:8080/rubyfaq/rubyfaq-5.html#ss5.3
|and thought to try. I didn't manage to make "auto(in|de)crement" working so
|could somebody help here? Does this contain some errors or is the idea
|wrong?

  (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.
Brian
2 and 3 seem contradictory. If self assignment is bad, why are `+=`/`-=` ok? And wouldn't `1+=1` be just as bad? (It fails in IRB with `syntax error, unexpected ASSIGNMENT`)
Andy_Vulhop
(2) means that in C, you're not altering the value itself...you're altering the contents of the variable that holds the value. That's a bit too meta for any language that passes by value. Unless there's a way to pass something by reference in Ruby (and i mean truly "by reference", not passing a reference by value), altering the variable itself wouldn't be possible within a method.
cHao
Maybe I am missing something here. `+=` replaces the object the variable references with a whole new object. You can check this by calling `i.object_id` before and after `i+=1`. Why would that be any more technically tricky to do with `++`?
Andy_Vulhop
@Andy_Vulhop: #3 is explaining why it's technically impossible for assignment to be a method, not why assignment is impossible in general (the poster Matz was replying to thought it might be possible to create a `++` method).
Chuck
+1  A: 

I think Matz' reasoning for not liking them is that it actually replaces the variable with a new one.

ex:

a = SomeClass.new
def a.go
  'hello'
end
# at this point, you can call a.go
# but if you did an a++
# that really means a = a + 1
# so you can no longer call a.go
# as you have lost your original

Now if somebody could convince him that it should just call #succ! or what not, that would make more sense, and avoid the problem. You can suggest it on ruby core.

rogerdpack
"You can suggest it on ruby core" ... *After* you have read *and* understood the arguments in all the other threads where it was suggested last time, and the time before that, and the time before that, and the time before that, and the time before that, and ... I haven't been in the Ruby community very long, but just during my time, I remember at least twenty such discussions.
Jörg W Mittag
+5  A: 

It's not conventional in OO languages. In fact, there is no ++ in Smalltalk, the language that coined the term "object-oriented programming" (and the language Ruby is most strongly influenced by). What you mean is that it's conventional in C and languages closely imitating C. Ruby does have a somewhat C-like syntax, but it isn't slavish in adhering to C traditions.

As for why it isn't in Ruby: Matz didn't want it. That's really the ultimate reason.

The reason no such thing exists in Smalltalk is because it's part of the language's overriding philosophy that assigning a variable is fundamentally a different kind of thing than sending a message to an object — it's on a different level. This thinking probably influenced Matz in designing Ruby.

It wouldn't be impossible to include it in Ruby — you could easily write a preprocessor that transforms all ++ into +=1. but evidently Matz didn't like the idea of an operator that did a "hidden assignment." It also seems a little strange to have an operator with a hidden integer operand inside of it. No other operator in the language works that way.

Chuck
+3  A: 

I think there's another reason: ++ in Ruby wouldn't be remotely useful as in C and its direct successors. The reason being the for keyword: while it's essential in C, it's mostly superfluous in Ruby - most of the iteration in Ruby is done through Enumerable methods, such as each and map - when iterating through some data structure, and Fixnum#times method, when you need to loop an exact number of times. Actually, as far as I have seen, most of the time +=1 is used by people freshly migrated to Ruby from C-style languages.

In short, it's really questionable if methods ++ and -- would be used at all.

Mladen Jablanović