Why does this throw a syntax error? I would expect it to be the other way around...
>> foo = 5
>> foo = foo++ + ++foo
There's no ++
operator in Ruby. Ruby is taking your foo++ + ++foo
and taking the first of those plus signs as a binary addition operator, and the rest as unary positive operators on the second foo
.
So you are asking Ruby to add 5 and (plus plus plus plus) 5, which is 5, hence the result of 10.
When you add the parentheses, Ruby is looking for a second operand (for the binary addition) before the first closing parenthesis, and complaining because it doesn't find one.
Where did you get the idea that Ruby supported a C-style ++
operator to begin with? Throw that book away.