Ruby 系统调用在脚本完成之前执行
我有一个 Ruby 脚本,它使用 erb 模板生成 Latex 文档。生成 .tex 文件后,我想进行系统调用以使用 pdflatex 编译文档。以下是脚本的主要内容:
class Book
# initialize the class, query a database to get attributes, create the book, etc.
end
my_book = Book.new
tex_file = File.open("/path/to/raw/tex/template")
template = ERB.new(tex_file.read)
f = File.new("/path/to/tex/output.tex")
f.puts template.result
system "pdflatex /path/to/tex/output.tex"
system
行使我进入交互式 tex 输入模式,就好像文档是空的一样。如果我删除呼叫,文档将正常生成。如何确保在生成文档之后才进行系统调用?与此同时,我只是使用一个 bash 脚本调用 ruby 脚本,然后调用 pdflatex 来解决这个问题。
I have a Ruby script that produces a Latex document using an erb template. After the .tex file has been generated, I'd like to make a system call to compile the document with pdflatex
. Here are the bones of the script:
class Book
# initialize the class, query a database to get attributes, create the book, etc.
end
my_book = Book.new
tex_file = File.open("/path/to/raw/tex/template")
template = ERB.new(tex_file.read)
f = File.new("/path/to/tex/output.tex")
f.puts template.result
system "pdflatex /path/to/tex/output.tex"
The system
line puts me in interactive tex input mode, as if the document were empty. If I remove the call, the document is generated as normal. How can I ensure that the system call isn't made until after the document is generated? In the meantime I'm just using a bash script that calls the ruby script and then pdflatex
to get around the issue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
File.new
将打开一个新流,该流不会关闭(保存到磁盘),直到脚本结束或您手动关闭它。这应该有效:
或者更友好的方式:
带有块的
File.open
将打开流,使流可以通过块变量访问(本例中为f
)并在块执行后自动关闭流。'w'
将打开或创建文件(如果文件已存在,内容将被删除 => 文件将被截断)The
File.new
will open a new stream that won't be closed (saved to disk) until the script ends of until you manually close it.This should work:
Or a more friendly way:
The
File.open
with a block will open the stream, make the stream accessible via the block variable (f
in this example) and auto-close the stream after the block execution. The'w'
will open or create the file (if the file already exists the content will be erased => The file will be truncated)