るりまサーチ

最速Rubyリファレンスマニュアル検索!
22件ヒット [1-22件を表示] (0.039秒)
トップページ > クエリ:Array[x] > クエリ:to_csv[x]

別のキーワード

  1. _builtin to_s
  2. openssl to_der
  3. openssl to_s
  4. _builtin to_a
  5. openssl to_pem

ライブラリ

クラス

検索結果

Array#to_csv(**options) -> String (39143.0)

CSV.generate_line(self, options) と同様です。

...様です。

Array
オブジェクトを 1 行の CSV 文字列に変換するためのショートカットです。

@param options CSV.generate_line と同様のオプションを指定します。

//emlist[][ruby]{
require 'csv'

p [1, 'Matz', :Ruby, Date.new(1965, 4, 14)].to_csv...
...# => "1,Matz,Ruby,1965-04-14\n"
p [1, 'Matz', :Ruby, Date.new(1965, 4, 14)].to_csv(col_sep: ' ', row_sep: "\r\n") # => "1 Matz Ruby 1965-04-14\r\n"
//}


@see CSV.generate_line...
..., 4, 14)].to_csv(col_sep: ' ', row_sep: "\r\n") # => "1 Matz Ruby 1965-04-14\r\n"
//}

Ruby 3.0 (CSV 3.1.9) から、次のオプションが使えるようになりました。

//emlist[][ruby]{
require 'csv'

puts [1, nil].to_csv # => 1,
puts [1, nil].to_csv(write_nil...
..._value: "N/A") # => 1,N/A
puts [2, ""].to_csv # => 2,""
puts [2, ""].to_csv(write_empty_value: "BLANK") # => 2,BLANK
//}

@see CSV.generate_line...

CSV (24.0)

このクラスは CSV ファイルやデータに対する完全なインターフェイスを提供します。

..."data"]
csv << ["another", "row"]
# ...
end
//}

=== 一行変換

//emlist[][ruby]{
require 'csv'

csv_string = ["CSV", "data"].to_csv # => "CSV,data"
csv_array = "CSV,String".parse_csv # => ["CSV", "String"]
//}

=== ショートカット

//emlist[][ruby]{
require 'csv'

CSV...
...data is never transcoded
(unless you ask Ruby to transcode it for you) and will literally be parsed in
the Encoding it is in. Thus CSV will return Arrays or Rows of Strings in the
Encoding of your data. This is accomplished by transcoding the parser itself
into your Encoding.

Some transcoding mus...
...is not ASCII compatible. There's no existing data for CSV to use to
prepare itself and thus you will probably need to manually specify the desired
Encoding for most of those cases. It will try to guess using the fields in a
row of output though, when using CSV::generate_line() or Array#to_csv()....