write(path, string, **opts) -> IntegerRuby 1.9.3 から[permalink][rdoc][edit]write(path, string, offset=nil, **opts) -> Integer-
path で指定されるファイルを開き、string を書き込み、閉じます。
Kernel.#open と同様 path の先頭が "|" ならば、"|" に続くコマンドを実行し、コマンドの出力を標準出力に書き込みます。
offset を指定するとその位置までシークします。
offset を指定しないと、書き込みの末尾でファイルを切り捨てます。ただし、キーワード引数 mode に "a" (追記モード) を指定した場合は、ファイルは切り詰められず、既存の内容の末尾に string が追記されます。
キーワード引数はファイルを開くときに使われ、エンコーディングなどを指定できます。詳しくは IO.open を見てください。
- [PARAM]
path: - ファイル名文字列
- [PARAM]
string: - 書き込む文字列
- [PARAM]
offset: - 書き込み開始位置
- [PARAM]
opts: - ファイルを開くときのキーワード引数
例: mode: "a" を指定した場合は切り詰められないtext = "This is line one\nThis is line two\nThis is line three\nAnd so on...\n" p IO.write("testfile", text) # => 66 p IO.write("testfile", "0123456789", 20) #=> 10 p IO.read("testfile") # => "This is line one\nThi0123456789two\nThis is line three\nAnd so on...\n" p IO.write("testfile", "0123456789") #=> 10 p IO.read("testfile") # => "0123456789"IO.write("testfile", "This is line one\n") IO.write("testfile", "This is line two\n", mode: "a") p IO.read("testfile") # => "This is line one\nThis is line two\n"[SEE_ALSO] IO.binwrite
- [PARAM]