73

In Java, something like i++ would increment i by 1.

How can I do in Ruby? Surely there has to be a better way than i = i + 1?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Carter Shaw
  • 849
  • 1
  • 6
  • 12
  • 1
    This doesn't show a lot of effort. Google would have turned up numerous hits for "[ruby increment operator](https://www.google.com/search?q=ruby+increment+operator)" including "[No increment operator (++) in Ruby?](http://stackoverflow.com/questions/3717519/no-increment-operator-in-ruby)", plus the afore-mentioned question. – the Tin Man Sep 30 '13 at 18:30
  • 2
    I think this is a different question. He's asking how you do it, not why there is no support for it. – Tony Feb 11 '14 at 18:56

1 Answers1

157

From the documentation,

Ruby has no pre/post increment/decrement operator. For instance, x++ or x-- will fail to parse

So, you can do

i += 1

which is equivalent of i = i + 1

karthikr
  • 97,368
  • 26
  • 197
  • 188