加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-17 01:46:33 所属栏目:百科 来源:网络整理
导读:我的rails应用程序今天才开始收到此错误.这是代码上下文.它在以new_host_id开头的行上抛出错误 while @host_ids.include?(new_host_id) i++ new_host_id = duplicate_host_id + i.to_send 解决方法 Ruby没有操作符. Ruby中的习语是i = 1,这是i = i 1的缩写形
我的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()..

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读