Ruby 意外的关键字结束错误,但语法似乎没有问题
此函数用于从逗号分隔的值文件中提取名称,并将其放入数组中Ruby 意外的关键字结束错误,但语法似乎没有问题,ruby,syntax-error,Ruby,Syntax Error,此函数用于从逗号分隔的值文件中提取名称,并将其放入数组中 def xprt_csv_to_ary(csv_file) namecatcher_regex = "/^[\.{1}]([A-Z]+)\.{3}/" # Matches up to char before next name current_word = 0 names_array = [] while current_word < 5000 if current_word == 0
def xprt_csv_to_ary(csv_file)
namecatcher_regex = "/^[\.{1}]([A-Z]+)\.{3}/" # Matches up to char before next name
current_word = 0
names_array = []
while current_word < 5000
if current_word == 0
name = csv_file.readline.match(namecatched_regex)
else
name = csv_file.past_match.match(namecatcher_regex)
end
names_array[current_word] = name
current_word ++
end
return names_array
end
如果有人直接回答我的问题,我也会很高兴能找到解决我问题的现有问题。您的错误来自第行:
current_word ++
Ruby中没有这样的语法。应该是:
current_word += 1
namecatcher_regex = /^[\.{1}]([A-Z]+)\.{3}/
而且,您创建的regexp不正确。应该是:
current_word += 1
namecatcher_regex = /^[\.{1}]([A-Z]+)\.{3}/
可能还有一些我没有注意到的错误 在这一行:
current_word ++
您正在告诉Ruby向当前单词添加内容,但您从未告诉它要添加什么,而是在下一行直接显示一个结尾。一元+
缺少操作数。应该是这样的
current_word + something_else
或
在Ruby中,运算符周围允许空白,因此
a +
b
# equivalent to a + b, which is equivalent to a.+(b)
甚至
+
a
# equivalent to +a, which is equivalent to a.+@()
很好,所以如果你把两者结合起来,你就得到了
a + +
b
# equivalent to a + +b, which is equivalent to a.+(b.+@())
也非常好,而且由于操作数周围的空格非常好,但是可选的
a+b
及
也很好
这就是为什么下一行只有end
出现错误的原因,因为只有Ruby才能判断出一元前缀+
运算符缺少操作数。非常感谢!我想我可以从这里开始标题和问题中的错误消息不匹配。是哪一个?是否缺少结尾
或结尾太多?
a ++
b
# equivalent to a + +b as above