Ganesh Shankar
Ganesh Shankar

Reputation: 4864

Does Ruby/Rails have a ++ equivalent?

I guess I just got used to saying things like:

x++

in PHP and Java land. But when I tried this in my Rails code it had a fit:

compile error
/users/gshankar/projects/naplan/app/views/answers/new.html.haml:19: syntax error, unexpected ';'
/users/gshankar/projects/naplan/app/views/answers/new.html.haml:23: syntax error, unexpected kENSURE, expecting kEND
...}\n", 0, false);_erbout;ensure;@haml_buffer = @haml_buffer.u...
                              ^
/users/gshankar/projects/naplan/app/views/answers/new.html.haml:26: syntax error, unexpected $end, expecting kEND

I googled around a bit for Ruby/Rails operators for a reference to ++ but couldn't find anything. For a language which focuses on writing less I find it a bit strange that there wouldn't be a ++ equivalent. Am I missing something?

Upvotes: 9

Views: 7738

Answers (4)

EmFi
EmFi

Reputation: 23450

While as other answers point out x += 1 and x -= 1 is one way to do this. The closest Ruby gets to the --/++ operators are the prev()/next() (next has an alias of succ()) methods which return the previous/next items in sequence and are available on unambiguous strictly ordered classes (like String).

x = 4     # => 4
x.succ    # => 5
x.prev    # => 3

y = 'ABC' # => 'ABC'
y.succ    # => 'ABD'
y.prev    # => 'ABB'

Unfortunately, none of these implicitly do assignment which is what the original question was asking about. igul222's answer links to a post from Ruby's creator explaining why this is not possible.

Ruby has very powerful collection processing capabilities that eliminate most needs for these kinds of assignments. In all but the most complex alogrithms, which involve processing multiple collections at different rates in parallel. You should be asking yourself why you need increment/decrement. Because the Ruby way to do operations where increment and decrement operators are commonly used is with an iterator.

Such as each_with_index which executes a given block for each element/index pair in the array.

For example, this code will iterate through an array outputting the string representation fo each element and the parity of its index.

array = ["first", "second", "third","fourth","last"]
array.each_with_index do |object,index|
   puts index % 2 ? "even" : "odd"
   puts object.to_s
end

Edit: Removed Fixnum extension providing postfix increment because it doesn't work. Upon closer inspection there's nothing stopping you from adding it, but it would involve writing your feature in C, and a recompile.

Edit 2022: Added comment about prev/next/succ functions which are closer in behaviour to the decrement/increment function of --/++ operators

Upvotes: 2

igul222
igul222

Reputation: 8637

x+=1 is the best you can do in Ruby.

For a detailed explanation, see Why doesn't Ruby support i++ or i--​ (increment/decrement operators)?

Upvotes: 5

Trevor
Trevor

Reputation: 6689

Try this:

x += 1

Upvotes: 16

thebretness
thebretness

Reputation: 632

You have to use x+=1 instead.

http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Operators

Upvotes: 1

Related Questions