Here document 在 Ruby IO 中给出了 EOF 错误
Here document gives EOF error in Ruby IO
以下代码给出了两个我无法解决的错误。任何帮助将不胜感激:
random.rb:10: can't find string "TEMPLATE" anywhere before EOF
random.rb:3: syntax error, unexpected end-of-input
代码:
id = 2
File.open("#{id}.json","w") do |file|
file.write <<TEMPLATE
{
"submitter":"#{hash["submitter"]}",
"quote":"#{hash["quote"]}",
"attribution":"#{hash["attribution"]}"
}
TEMPLATE
end
From the documentation(强调我的):
The heredoc starts on the line following <<HEREDOC
and ends with the next line that starts with HEREDOC
您的代码不包含以 TEMPLATE
开头的行。如果您的文本编辑器(或 IDE)支持搜索中的正则表达式,请尝试 ^TEMPLATE
.
您可以删除空格,或者如果您想保留它们,请将 <<TEMPLATE
更改为 <<-TEMPLATE
。 -
的添加指示 Ruby 解析器搜索(可能)预期的 TEMPLATE
,就像您在代码中那样。
以下代码给出了两个我无法解决的错误。任何帮助将不胜感激:
random.rb:10: can't find string "TEMPLATE" anywhere before EOF
random.rb:3: syntax error, unexpected end-of-input
代码:
id = 2
File.open("#{id}.json","w") do |file|
file.write <<TEMPLATE
{
"submitter":"#{hash["submitter"]}",
"quote":"#{hash["quote"]}",
"attribution":"#{hash["attribution"]}"
}
TEMPLATE
end
From the documentation(强调我的):
The heredoc starts on the line following
<<HEREDOC
and ends with the next line that starts withHEREDOC
您的代码不包含以 TEMPLATE
开头的行。如果您的文本编辑器(或 IDE)支持搜索中的正则表达式,请尝试 ^TEMPLATE
.
您可以删除空格,或者如果您想保留它们,请将 <<TEMPLATE
更改为 <<-TEMPLATE
。 -
的添加指示 Ruby 解析器搜索(可能)预期的 TEMPLATE
,就像您在代码中那样。