Ruby如果Elsif Else阻止语法

时间:2015-07-28 19:42:48

标签: ruby if-statement

这个街区出了什么问题?

if item.nil?
   found = true
elsif item[:name] == name && item[:category] == category
   found = true
else
   i++
end

当我进行语法检查时,我得到了

syntax error, unexpected keyword_end

但如果我删除end,那么我会

syntax error, unexpected end-of-input

1 个答案:

答案 0 :(得分:6)

问题在于:

i++

Ruby没有++运算符。你想要的是这个:

i += 1

我认为这个特定错误的原因是Ruby将第二个+解释为一元+运算符,即“正数符号”(因为它是唯一有意义的东西)那个上下文),所以期望下一个标记是一个数字,例如(为清晰起见括号):

i + (+5)

...但下一个标记是end,错误是语法错误。

或其他responds to the +@ method