未定义的方法'+@' - 红宝石错误

发布于 11-16 17:00 字数 948 浏览 2 评论 0原文

我一直在尝试用 Ruby 编写一个类似于以下内容的 while 循环:

number = 3
string = "lalaLand"
def main(number, string)
    n = 1
    while n <= number do
        print string + 'something else' + n.to_s
        n = n + 1
    end
end
main(number, string)

我不断收到错误:

...: undefined method `+@' for "lalaLand":String (NoMethodError)

任何人都可以帮我解决这个问题吗? 谢谢

----编辑---(实际代码,也许我错过了一些东西)

devel_number是唯一声明为int的(即:devel_number = 4,其余的都是字符串:project_name = "lalaLand") ...

def b_sani_dev(project_home, source_dir, project_name, devel_number)

    n = 1
    while n <= devel_number do
        system 'mkdir -p ' + project_home + 'dev_'+ n.to_s + '/main'
        system 'mkdir -p ' + project_home + 'dev_' + n.to_s + '/LOG'
        n = n + 1
    end            
    b_sani_dev(project_home, source_dir, project_name, devel_number)

I have been trying to write a while loop in Ruby similar to the following:

number = 3
string = "lalaLand"
def main(number, string)
    n = 1
    while n <= number do
        print string + 'something else' + n.to_s
        n = n + 1
    end
end
main(number, string)

And I keep getting the error:

...: undefined method `+@' for "lalaLand":String (NoMethodError)

Can anyone help me with this?
Thanks

----EDIT--- (actual code, maybe there is something I missed)

devel_number is the only one declared as an int (ie: devel_number = 4, the rest are strings: project_name = "lalaLand")
...

def b_sani_dev(project_home, source_dir, project_name, devel_number)

    n = 1
    while n <= devel_number do
        system 'mkdir -p ' + project_home + 'dev_'+ n.to_s + '/main'
        system 'mkdir -p ' + project_home + 'dev_' + n.to_s + '/LOG'
        n = n + 1
    end            
    b_sani_dev(project_home, source_dir, project_name, devel_number)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柠栀2024-11-23 17:00:22

您发布的所有代码都应该可以正常工作,但错误源于尝试在字符串上使用一元 + 运算符,这是非法的。

+17   # totally fine
+"hi" # throws an error; what does this even mean?

All of the code you've posted should work fine but the error stems from attempting to use the unary + operator on a string, which is illegal.

+17   # totally fine
+"hi" # throws an error; what does this even mean?
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文