29

What causes the error in the following code?

ruby -e "puts 1++"
-e:1: syntax error, unexpected $end

or

ruby -e "x=1; puts x++;"
-e:1: syntax error, unexpected ';'
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
robert
  • 8,459
  • 9
  • 45
  • 70

1 Answers1

65

Ruby doesn't have an ++ operator. You can do puts 1.next though. Note that for your second example this would not change the value of x, in that case you'd have to use x += 1.

Community
  • 1
  • 1
Michael Kohl
  • 66,324
  • 14
  • 138
  • 158
  • 1
    you mean, ruby doesn't have ++ operator? – robert Nov 03 '11 at 11:17
  • 3
    Exactly, it has neither an increment nor a decrement operator. Look at this post to see why: http://stackoverflow.com/questions/3660563/why-doesnt-ruby-support-i-or-i-for-fixnum – Michael Kohl Nov 03 '11 at 11:21