Breaking Java Habits: Ruby increment/decrement operators

My background in C++ and Java has instilled in me some programming habits that don’t play nice in Ruby. When I want to increment or decrement a number, my fingers instinctively go to ‘++’ and ‘–’’.

Unfortunately, this syntax is not supported by Ruby. As a language design choice, Ruby opted to not support this syntactic sugar to increment/decrement a variable.

I will have to break my habit and start going with:

// increment x
x+=1
// decrement x
x-=1

Another thing to note is that the prefix increment/decrement operators like ‘–x’ or ‘++x’ do nothing. The signs are interpreted as unary operators.

Breaking Java Habits: Ruby increment/decrement operators

2 thoughts on “Breaking Java Habits: Ruby increment/decrement operators

  1. Kevin McCaughey says:

    lol – I found this when trying to find out if it did support increment/decrement 😉 Maybe in 2.0

Leave a Reply

Your email address will not be published. Required fields are marked *