tempfile
が巨大な場合、FileUtils.cp
のコストが高い
たまによくあるこんなコード
require 'tempfile'
require 'fileutils'
Tempfile.create('hoge') do |tempfile|
# ...
# 処理が成功した場合は、result_file に tempfile の中身を入れたい
FileUtils.cp(result_file, tempfile) if success?
end
解決案
FileUtils.ln
を使えばコストは copy より軽くなる
- ハードリンクなので、
Tempfile.create
ブロック終了時にtempfile
が unlink されてもresult_file
は削除されない
require 'tempfile'
require 'fileutils'
Tempfile.create('hoge') do |tempfile|
# ...
# 処理が成功した場合は、result_file に tempfile の中身を入れたい
FileUtils.ln(result_file, tempfile) if success?
end
こういう場合のためのメソッドが欲しいというrubyのissueがあったけど、進捗はない様子