微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐