未定义的方法'+@' - 红宝石错误
我一直在尝试用 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)
您发布的所有代码都应该可以正常工作,但错误源于尝试在字符串上使用一元
+
运算符,这是非法的。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.