我使用PHP生成一些特殊格式的文件,我决定用Ruby来尝试同样的方法。要用PHP生成文件,我使用以下代码:
<? include 'functions_and_settings.php'; ob_start() ?>
some parts of another format
<? // php functions generating file content,
// including other formatted files ?>
some parts
<? file_put_contents('output.fmt', ob_get_clean()) ?>
可以用红宝石吗?你会怎么做?
更新
以下代码相当于php代码:
require 'erb'
require 'my_functions_and_settings'
template = ERB.new <<-EOF
some text lines of another format
<% #functions generating content,
# inclusion of formatted files %>
some text lines of another format
EOF
File.open("output.fmt", "w") do |f|
f.puts template.result(binding)
end
# or may be: File.new("file.txt") << template.result(binding)
有办法吗
ruby file.erb >> output.fmt
?
更新2
标准Ruby发行版
erb
处理器
/usr/bin/erb my_formatted_file.erb