Ruby Parenthesis syntax exception with i++ ++i

后端 未结 3 685
野趣味
野趣味 2021-01-14 17:05

Why does this throw a syntax error? I would expect it to be the other way around...

>> foo = 5
>> foo = foo++ + ++foo                                     


        
3条回答
  •  忘掉有多难
    2021-01-14 17:30

    Ruby does not support this syntax. Use i+=1 instead.

    As @Dylan mentioned, Ruby is reading your code as foo + (+(+(+(+foo)))). Basically it's reading all the + signs (after the first one) as marking the integer positive.

提交回复
热议问题