printは改行を出力するという記事をどこかで見かけたので、やってみたら、
改行が出力されなかった。。
ということで、
Ruby-Doc.org
で、きちんと調べてみる。
1.putsについて
http://www.ruby-doc.org/core-1.9.3/IO.html#method-i-puts
Writes the given objects to ios as with IO#print. Writes a record separator (typically a newline) after any that do not already end with a newline sequence. If called with an array argument, writes each element on a new line. If called without arguments, outputs a single record separator.
・複数のオブジェクト(配列で渡すことも可)を書き込むことができる
・それぞれの末尾が、改行で終わっていなければ、改行をつける
ということらしい。なるほど。
と書くと、
と出力された。
1行目の[CR][LF]は、putsが自動的につけたもの。
2行目は、"bbb\n"には既に改行がついているので、重複して改行はつけない。(\nが[CR][LF]で出力されるが。。)
3行目も、既に改行がついているので、重複して改行はつけないが、\rは[CR]で、\nが[CR][LF]で出力されるため、このようになる。
\nが[CR][LF]で出力されるのは気になるが一旦納得した。
2.printについて
http://www.ruby-doc.org/core-1.9.3/IO.html#method-i-print
Writes the given object(s) to ios. The stream must be opened for writing. If the output field separator ($,) is not nil, it will be inserted between each object. If the output record separator ($\) is not nil, it will be appended to the output. If no arguments are given, prints $_. Objects that aren’t strings will be converted by calling their to_s method. With no argument, prints the contents of the variable $_. Returns nil.
・複数のオブジェクトを書き込むことができる
・フィールドセパレータを「$,」で、レコードセパレータを「$\」で指定することができる(指定しなければ何も出力しない)
・配列は渡せない(何も書いてないが、やってみたら無理だった)
ということらしい。
と書くと、
と出力された。
配列が指定できれば、使い道は広そうなんだけど。。
ちなみに、ここでも、レコードセパレータには\nを指定したはずだが、出力は[CR][LF]となった。
3.writeについて
http://www.ruby-doc.org/core-1.9.3/IO.html#method-c-write
Opens the file, optionally seeks to the given offset, writes string, then returns the length written. write ensures the file is closed before returning. If offset is not given, the file is truncated. Otherwise, it is not truncated.
・単一の文字列をファイルに書き出す
・オフセットの指定ができる
ということで一番シンプル。
と書くと、
と出力され、\nはやっぱり[CR][LF]となった。
所感:
・1行出力するのにputsは使い方的に違う気がしてきた。
・printは配列を指定できるなら、使える気もするが、そうでないなら、いちいちセパレータを制御するのは面倒臭い。
・結局、writeを使うのが一番問題無いか。。
改行が出力されなかった。。
ということで、
Ruby-Doc.org
で、きちんと調べてみる。
1.putsについて
http://www.ruby-doc.org/core-1.9.3/IO.html#method-i-puts
Writes the given objects to ios as with IO#print. Writes a record separator (typically a newline) after any that do not already end with a newline sequence. If called with an array argument, writes each element on a new line. If called without arguments, outputs a single record separator.
・複数のオブジェクト(配列で渡すことも可)を書き込むことができる
・それぞれの末尾が、改行で終わっていなければ、改行をつける
ということらしい。なるほど。
File.open("test.txt", "w:UTF-8") {|wf|
arr = ["aaa","bbb\n","ccc\r\n"]
wf.puts(arr)
}と書くと、
aaa[CR][LF]
bbb[CR][LF]
ccc[CR][CR][LF]
と出力された。
1行目の[CR][LF]は、putsが自動的につけたもの。
2行目は、"bbb\n"には既に改行がついているので、重複して改行はつけない。(\nが[CR][LF]で出力されるが。。)
3行目も、既に改行がついているので、重複して改行はつけないが、\rは[CR]で、\nが[CR][LF]で出力されるため、このようになる。
\nが[CR][LF]で出力されるのは気になるが一旦納得した。
2.printについて
http://www.ruby-doc.org/core-1.9.3/IO.html#method-i-print
Writes the given object(s) to ios. The stream must be opened for writing. If the output field separator ($,) is not nil, it will be inserted between each object. If the output record separator ($\) is not nil, it will be appended to the output. If no arguments are given, prints $_. Objects that aren’t strings will be converted by calling their to_s method. With no argument, prints the contents of the variable $_. Returns nil.
・複数のオブジェクトを書き込むことができる
・フィールドセパレータを「$,」で、レコードセパレータを「$\」で指定することができる(指定しなければ何も出力しない)
・配列は渡せない(何も書いてないが、やってみたら無理だった)
ということらしい。
# coding: utf-8
File.open("test.txt", "w:UTF-8") {|wf|
$,=","
$\="\n"
wf.print("aaa","bbb","ccc")
wf.print("ddd","eee","fff")
}
と書くと、
aaa,bbb,ccc[CR][LF]
ddd,eee,fff[CR][LF]
と出力された。
配列が指定できれば、使い道は広そうなんだけど。。
ちなみに、ここでも、レコードセパレータには\nを指定したはずだが、出力は[CR][LF]となった。
3.writeについて
http://www.ruby-doc.org/core-1.9.3/IO.html#method-c-write
Opens the file, optionally seeks to the given offset, writes string, then returns the length written. write ensures the file is closed before returning. If offset is not given, the file is truncated. Otherwise, it is not truncated.
・単一の文字列をファイルに書き出す
・オフセットの指定ができる
ということで一番シンプル。
# coding: utf-8
File.open("test.txt", "w:UTF-8") {|wf|
wf.write("aaa\nbbb\nccc\n")
}
と書くと、
aaa[CR][LF]
bbb[CR][LF]
ccc[CR][LF]
と出力され、\nはやっぱり[CR][LF]となった。
所感:
・1行出力するのにputsは使い方的に違う気がしてきた。
・printは配列を指定できるなら、使える気もするが、そうでないなら、いちいちセパレータを制御するのは面倒臭い。
・結局、writeを使うのが一番問題無いか。。