Does Ruby / Rails have a ++ equivalent?

I think I'm just used to saying things like:

x++

in PHP and Java. 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 did a bit of work for the Ruby / Rails statements to reference ++, but didn't find anything. For a language that focuses less on writing, I find it a bit strange that there would be no ++ equivalent. Did I miss something?

+5
source share
4 answers

Try the following:

x += 1
+12
source

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

For a detailed explanation, see Why Ruby does not support i ++ or i-- (increase / decrease operators)?

+4
source

, , /. Ruby , . , , Ruby- , , - .

, each_with_index, / .

, , .

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

: Fixnum, , . , C .

+3
source

All Articles