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()) ?>
Rubyでできますか?これをどのように行いますか?
アップデート
次のコードは、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
か?
Update2
標準の Ruby ディストリビューションにはerb
プロセッサがあります
/usr/bin/erb my_formatted_file.erb