ruby – NoMethodError:未定义的方法`@’表示“some sting”:String

我的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()..

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...