Ruby 1.8.7 リファレンスマニュアル > ライブラリ一覧 > tempfileライブラリ > Tempfileクラス > new
new(basename, tempdir = Dir::tmpdir) -> Tempfileopen(basename, tempdir = Dir::tmpdir) -> Tempfileopen(basename, tempdir = Dir::tmpdir) {|fp| ...} -> nilテンポラリファイルを作成し、それを表す Tempfile オブジェクトを生成して返します。 ファイル名のプレフィクスには指定された basename が使われます。 ファイルは指定された tempdir に作られます。 ブロックを指定して呼び出した場合は、Tempfile オブジェクトを引数として ブロックを実行します。ブロックの実行が終了すると、ファイルは自動的に クローズされ、 nilをかえします。
例:
require "tempfile" t = Tempfile.open(['hoge', 'bar']) p t.path #=> "/tmp/hoge20080518-6961-5fnk19-0bar" t2 = Tempfile.open(['t', '.xml']) p t2.path #=> "/tmp/t20080518-6961-xy2wvx-0.xml"
例:ブロックを与えた場合
require 'tempfile'
path = nil
Tempfile.open("temp"){|fp|
fp.puts "hoge"
path = fp.path
}
system("cat #{path}")