Ruby 1.9.2 リファレンスマニュアル > ライブラリ一覧 > csvライブラリ > CSVクラス > new
new(data, options = Hash.new) -> CSV
このメソッドは CSV ファイルを読み込んだり、書き出したりするために String か IO のインスタンスをラップします。
ラップされた文字列の先頭から読み込むことになります。 文字列に追記したい場合は CSV#generate を使用してください。 他の位置から処理したい場合はあらかじめそのように設定した StringIO を渡してください。
Note that a wrapped String will be positioned at at the beginning (for reading). If you want it at the end (for writing), use CSV::generate(). If you want any other positioning, pass a preset StringIO object instead.
フィールドの区切り文字列を指定します。この文字列はパースする前にデータの エンコーディングに変換されます。
行区切りの文字列を指定します。:auto という特別な値をセットすることができます。 :auto を指定した場合データから自動的に行区切りの文字列を見つけ出します。このとき データの先頭から次の "\r\n", "\n", "\r" の並びまでを読みます。 A sequence will be selected even if it occurs in a quoted field, assuming that you would have the same line endings there. If none of those sequences is found, +data+ is <tt>ARGF</tt>, <tt>STDIN</tt>, <tt>STDOUT</tt>, or <tt>STDERR</tt>, or the stream is only available for output, the default <tt>$INPUT_RECORD_SEPARATOR</tt> (<tt>$/</tt>) is used. Obviously, discovery takes a little time. Set manually if speed is important. Also note that IO objects should be opened in binary mode on Windows if this feature will be used as the line-ending translation can cause problems with resetting the document position to where it was before the read ahead. This String will be transcoded into the data's Encoding before parsing.
フィールドをクオートする文字を指定します。長さ 1 の文字列でなければなりません。 正しいダブルクオートではなく間違ったシングルクオートを使用しているアプリケーション で便利です。 CSV will always consider a double sequence this character to be an escaped quote. この文字列はパースする前にデータのエンコーディングに変換されます。
This is a maximum size CSV will read ahead looking for the closing quote for a field. (In truth, it reads to the first line ending beyond this size.) If a quote cannot be found within the limit CSV will raise a MalformedCSVError, assuming the data is faulty. You can use this limit to prevent what are effectively DoS attacks on the parser. However, this limit can cause a legitimate parse to fail and thus is set to +nil+, or off, by default.
An Array of names from the Converters Hash and/or lambdas that handle custom conversion. A single converter doesn't have to be in an Array. All built-in converters try to transcode fields to UTF-8 before converting. The conversion will fail if the data cannot be transcoded, leaving the field unchanged.
If set to +true+, an unconverted_fields() method will be added to all returned rows (Array or CSV::Row) that will return the fields as they were before conversion. Note that <tt>:headers</tt> supplied by Array or String were not fields of the document and thus will have an empty Array attached.
:first_row というシンボルか真を指定すると、CSV ファイルの一行目をヘッダとして扱います。 配列を指定するとそれをヘッダとして扱います。文字列を指定すると CSV.parse_line を 使用してパースした結果をヘッダとして扱います。このとき、:col_sep, :row_sep, :quote_char はこのインスタンスと同じものを使用します。 This setting causes CSV#shift() to return rows as CSV::Row objects instead of Arrays and CSV#read() to return CSV::Table objects instead of an Array of Arrays.
When +false+, header rows are silently swallowed. If set to +true+, header rows are returned in a CSV::Row object with identical headers and fields (save that the fields do not go through the converters).
真を指定して :headers にも値をセットすると、ヘッダを出力します。
Identical in functionality to <tt>:converters</tt> save that the conversions are only made to header rows. All built-in converters try to transcode headers to UTF-8 before converting. The conversion will fail if the data cannot be transcoded, leaving the header unchanged.
真を指定すると、空行を読み飛ばします。
真を指定すると、全てのフィールドを作成時にクオートします。
[SEE_ALSO] CSV::DEFAULT_OPTIONS, CSV.open