Ruby 中的“语法错误,意外的 tCONSTANT”错误是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7317332/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 04:18:59  来源:igfitidea点击:

What is "Syntax Error, unexpected tCONSTANT" error in Ruby?

rubylearn-ruby-the-hard-way

提问by The Realmccoy

I am currently on Lesson 9 in "Learn Ruby the hard way".

我目前正在学习“艰难地学习 Ruby”中的第 9 课。

I have typed the the line number 6 exactly as the way its being instructed but still I am getting error while executing.

我已经完全按照指示的方式输入了第 6 行,但在执行时仍然出现错误。

It says:

它说:

Syntax error, unexpected tCONSTANT, expecting $end
puts " Here ^ are the days : ", days 

回答by Paul Annesley

You have forgotten to close a string on a previous line. Here's the problem reproduced:

您忘记关闭前一行的字符串。这是重现的问题:

paul@paulbookpro ~ ? ruby     
days = "abc
puts "Here are the days"
-:2: syntax error, unexpected tCONSTANT, expecting $end
puts "Here are the days"
          ^

It's treating the double-quote before the word "Here" as the closing quote of the string on the previous line, and then wondering why you're using a constant called Here (token beginning with upper case letter).

它将单词“Here”之前的双引号视为上一行字符串的结束引号,然后想知道为什么要使用称为 Here(以大写字母开头的标记)的常量。

回答by sepp2k

The error message means that the ruby parser encountered a constant (i.e. an identifier starting with a capital letter) where it did not expect one (specifically the parser expected the file to end at that point).

该错误消息意味着 ruby​​ 解析器遇到了一个常量(即以大写字母开头的标识符),而它并不期望有一个常量(特别是解析器期望文件在该点结束)。

Since the code you've shown does not even contain a constant, the problem is likely caused by another part of your code.

由于您显示的代码甚至不包含常量,因此问题很可能是由代码的另一部分引起的。