意外的keyword_end错误,但语法似乎很好
此函数应该从逗号分隔值文件中提取名称,并将它们放入数组中。
def xprt_csv_to_ary(csv_file) namecatcher_regex = "/^[\.{1}]([AZ]+)\.{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
我收到以下错误:
syntax error, unexpected keyword_end
我很乐意被提到一个解决我的问题的现有问题,就是让别人直接回答我。
你的错误来自一行:
current_word ++
Ruby中没有这样的语法。 它应该是:
current_word += 1
更重要的是,您错误地创建了正则表达式。 它应该是:
namecatcher_regex = /^[\.{1}]([AZ]+)\.{3}/
可能还有其他一些我没有注意到的错误。
在这一行:
current_word ++
你告诉Ruby要向current_word
添加一些东西,但是你永远不会告诉它要添加什么 ,而是直接在下一行上end
。 你错过了一元+
的操作数。 应该是这样的
current_word + something_else
要么
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
和
a ++ b # equivalent to a + +b as above
也很好。
这就是为什么你只能在下一行end
得到错误,因为只有Ruby可以告诉你缺少一元前缀+
运算符的操作数。