Increment variable in ruby

Ruby on-RailsRuby

Ruby on-Rails Problem Overview


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?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCarter ShawView Question on Stackoverflow
Solution 1 - Ruby on-RailskarthikrView Answer on Stackoverflow