Ruby 1.9.2 リファレンスマニュアル > ライブラリ一覧 > csvライブラリ > CSVクラス > new

singleton method CSV.new

new(data, options = Hash.new) -> CSV

このメソッドは CSV ファイルを読み込んだり、書き出したりするために StringIO のインスタンスをラップします。

ラップされた文字列の先頭から読み込むことになります。 文字列に追記したい場合は 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.

[PARAM] data:
StringIO のインスタンスを指定します。 String のインスタンスを指定した場合、CSV#string を使用して 後からデータを取り出すことが出来ます。
[PARAM] options:
CSV をパースするためのオプションをハッシュで指定します。 パフォーマンス上の理由でインスタンスメソッドではオプションを上書きすることが 出来ないので、上書きしたい場合は必ずここで上書きするようにしてください。
:col_sep

フィールドの区切り文字列を指定します。この文字列はパースする前にデータの エンコーディングに変換されます。

:row_sep

行区切りの文字列を指定します。: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.

:quote_char

フィールドをクオートする文字を指定します。長さ 1 の文字列でなければなりません。 正しいダブルクオートではなく間違ったシングルクオートを使用しているアプリケーション で便利です。 CSV will always consider a double sequence this character to be an escaped quote. この文字列はパースする前にデータのエンコーディングに変換されます。

:field_size_limit

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.

:converters

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.

:unconverted_fields

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.

:headers

: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.

:return_headers

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).

:write_headers

真を指定して :headers にも値をセットすると、ヘッダを出力します。

:header_converters

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.

:skip_blanks

真を指定すると、空行を読み飛ばします。

:force_quotes

真を指定すると、全てのフィールドを作成時にクオートします。

[EXCEPTION] CSV::MalformedCSVError:
不正な CSV をパースしようとしたときに発生します。

[SEE_ALSO] CSV::DEFAULT_OPTIONS, CSV.open