Unexpected keyword_end error, yet syntax seems fine

好久不见. 提交于 2019-11-27 05:42:43

Your error comes from line:

current_word ++

There's no such syntax in Ruby. It should be:

current_word += 1

What's more, you create your regexp incorrectly. It should be:

namecatcher_regex = /^[\.{1}]([A-Z]+)\.{3}/

There may be some other errors that I didn't notice.

On this line:

current_word ++

You are telling Ruby to add something to current_word, but you never tell it what to add, instead there's an end directly on the next line. You are missing the operand to the unary +. It should be something like

current_word + something_else

or

current_word + +something_else

In Ruby, whitespace is allowed around operators, so

a + 
b
# equivalent to a + b, which is equivalent to a.+(b)

and even

+ 
a
# equivalent to +a, which is equivalent to a.+@()

is perfectly fine, so if you combine the two, you get that

a + + 
b
# equivalent to a + +b, which is equivalent to a.+(b.+@())

is also perfectly fine, and since whitespace around operands is perfectly fine but optional,

a+b

and

a ++ 
b
# equivalent to a + +b as above

is also perfectly fine.

That's why you only get the error on the next line with the end, because only there can Ruby tell that you are missing the operand to the unary prefix + operator.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!