我的rails应用程序今天才开始收到此错误.这是代码上下文.它在以new_host_id开头的行上抛出错误
while @host_ids.include?(new_host_id) i++ new_host_id = duplicate_host_id + i.to_s end
解决方法
Ruby没有操作符.
Ruby中的习语是i = 1,这是i = i 1的缩写形式.
最初我认为发布的代码不正确,必须是我生成该错误.但是,正如JörgWMittag在评论中解释的那样,情况并非如此:
[..] Ruby allows whitespace (including line breaks) between an operator and the operand(s),so the entire thing is interpreted as
i + (+(new_host_id = duplicate_host_id + i.to_s))
[.. which is] why the NoMethodError refers to String.
> x = "hello" > +x undefined method `+@' for "hello":String (NoMethodError) > x+ Syntax error,unexpected $end
我使用而不是上面简化示例:Ruby将i和i视为制作(i)和[粗略] i()..