为什么我的程序被赋予一个意外的令牌:$end错误?



这是我未完成的代码:

#When convert button is pressed
File.rename("*.osz", "*.zip$")
dialog.directory(
def extract_zip(file, destination) FileUtils.mkdir_p(destination)
file_path = "./convert_temp/*.zip"
destination = "./convert_temp/osz/"
extract_zip(file_path, destination)
until File.exists?( ".osu$" ) == false do
File.rename("./convert_temp/osz/*.osu$", "*.txt$")
File.foreach(filename) do |file|
file_string = File.read('./convert_temp/osz/*.txt$')
if file_string.include?('Mode: 1')
puts 'Yes'
else
puts 'No'
end
end
end

机械战警给出以下语法错误:

unexpected token $end (Using Ruby 2.2 parser; configure using `TargetRubyVersion` parameter, under `AllCops`)

实际上,Rubocop 甚至无法解析文件,因为它有语法错误。

错误消息syntax error: unexpected token $end意味着 ruby 解析器正在愉快地解析,但随后它突然遇到了一个$end,这是解析器说"文件结束"的方式。它期待更多的代码,但它找到了文件的结尾。

这是正确缩进后代码的外观:

#When convert button is pressed
File.rename("*.osz", "*.zip$")
dialog.directory(
def extract_zip(file, destination) FileUtils.mkdir_p(destination)
file_path = "./convert_temp/*.zip"
destination = "./convert_temp/osz/"
extract_zip(file_path, destination)
until File.exists?( ".osu$" ) == false do
File.rename("./convert_temp/osz/*.osu$", "*.txt$")
File.foreach(filename) do |file|
file_string = File.read('./convert_temp/osz/*.txt$')
if file_string.include?('Mode: 1')
puts 'Yes'
else
puts 'No'
end
end
end

使用这种缩进可以很容易地看到有一些缺失的端点/括号,因为最后一行悬在空中,而不是闭合到它开始的左边缘。

附加说明:

dialog.directory(
def extract_zip(file, destination) FileUtils.mkdir_p(destination)

在方法调用中定义新方法是非常规的。File.open(def hello_world(..))没有多大意义。

until File.exists?( ".osu$" ) == false do

您是否使用$来指示"文件名以 .osu 结尾"?如果是,它不会那样工作。这将查找具有.osu$作为名称的文件。

File.foreach(filename) do |file|

file参数未在后面的块中使用,您使用file_string.

file_string = File.read('./convert_temp/osz/*.txt$')

你不能像这样一次读取多个文件。此外,上面的File.foreach将逐行读取文件,因此在这里您尝试在已经读取它的循环中再次读取它。

最新更新