Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/smanonr..../opt/alt/ruby31/share/ruby
File: csv.rb
# encoding: US-ASCII
[0] Fix | Delete
# frozen_string_literal: true
[1] Fix | Delete
# = csv.rb -- CSV Reading and Writing
[2] Fix | Delete
#
[3] Fix | Delete
# Created by James Edward Gray II on 2005-10-31.
[4] Fix | Delete
#
[5] Fix | Delete
# See CSV for documentation.
[6] Fix | Delete
#
[7] Fix | Delete
# == Description
[8] Fix | Delete
#
[9] Fix | Delete
# Welcome to the new and improved CSV.
[10] Fix | Delete
#
[11] Fix | Delete
# This version of the CSV library began its life as FasterCSV. FasterCSV was
[12] Fix | Delete
# intended as a replacement to Ruby's then standard CSV library. It was
[13] Fix | Delete
# designed to address concerns users of that library had and it had three
[14] Fix | Delete
# primary goals:
[15] Fix | Delete
#
[16] Fix | Delete
# 1. Be significantly faster than CSV while remaining a pure Ruby library.
[17] Fix | Delete
# 2. Use a smaller and easier to maintain code base. (FasterCSV eventually
[18] Fix | Delete
# grew larger, was also but considerably richer in features. The parsing
[19] Fix | Delete
# core remains quite small.)
[20] Fix | Delete
# 3. Improve on the CSV interface.
[21] Fix | Delete
#
[22] Fix | Delete
# Obviously, the last one is subjective. I did try to defer to the original
[23] Fix | Delete
# interface whenever I didn't have a compelling reason to change it though, so
[24] Fix | Delete
# hopefully this won't be too radically different.
[25] Fix | Delete
#
[26] Fix | Delete
# We must have met our goals because FasterCSV was renamed to CSV and replaced
[27] Fix | Delete
# the original library as of Ruby 1.9. If you are migrating code from 1.8 or
[28] Fix | Delete
# earlier, you may have to change your code to comply with the new interface.
[29] Fix | Delete
#
[30] Fix | Delete
# == What's the Different From the Old CSV?
[31] Fix | Delete
#
[32] Fix | Delete
# I'm sure I'll miss something, but I'll try to mention most of the major
[33] Fix | Delete
# differences I am aware of, to help others quickly get up to speed:
[34] Fix | Delete
#
[35] Fix | Delete
# === \CSV Parsing
[36] Fix | Delete
#
[37] Fix | Delete
# * This parser is m17n aware. See CSV for full details.
[38] Fix | Delete
# * This library has a stricter parser and will throw MalformedCSVErrors on
[39] Fix | Delete
# problematic data.
[40] Fix | Delete
# * This library has a less liberal idea of a line ending than CSV. What you
[41] Fix | Delete
# set as the <tt>:row_sep</tt> is law. It can auto-detect your line endings
[42] Fix | Delete
# though.
[43] Fix | Delete
# * The old library returned empty lines as <tt>[nil]</tt>. This library calls
[44] Fix | Delete
# them <tt>[]</tt>.
[45] Fix | Delete
# * This library has a much faster parser.
[46] Fix | Delete
#
[47] Fix | Delete
# === Interface
[48] Fix | Delete
#
[49] Fix | Delete
# * CSV now uses keyword parameters to set options.
[50] Fix | Delete
# * CSV no longer has generate_row() or parse_row().
[51] Fix | Delete
# * The old CSV's Reader and Writer classes have been dropped.
[52] Fix | Delete
# * CSV::open() is now more like Ruby's open().
[53] Fix | Delete
# * CSV objects now support most standard IO methods.
[54] Fix | Delete
# * CSV now has a new() method used to wrap objects like String and IO for
[55] Fix | Delete
# reading and writing.
[56] Fix | Delete
# * CSV::generate() is different from the old method.
[57] Fix | Delete
# * CSV no longer supports partial reads. It works line-by-line.
[58] Fix | Delete
# * CSV no longer allows the instance methods to override the separators for
[59] Fix | Delete
# performance reasons. They must be set in the constructor.
[60] Fix | Delete
#
[61] Fix | Delete
# If you use this library and find yourself missing any functionality I have
[62] Fix | Delete
# trimmed, please {let me know}[mailto:james@grayproductions.net].
[63] Fix | Delete
#
[64] Fix | Delete
# == Documentation
[65] Fix | Delete
#
[66] Fix | Delete
# See CSV for documentation.
[67] Fix | Delete
#
[68] Fix | Delete
# == What is CSV, really?
[69] Fix | Delete
#
[70] Fix | Delete
# CSV maintains a pretty strict definition of CSV taken directly from
[71] Fix | Delete
# {the RFC}[http://www.ietf.org/rfc/rfc4180.txt]. I relax the rules in only one
[72] Fix | Delete
# place and that is to make using this library easier. CSV will parse all valid
[73] Fix | Delete
# CSV.
[74] Fix | Delete
#
[75] Fix | Delete
# What you don't want to do is to feed CSV invalid data. Because of the way the
[76] Fix | Delete
# CSV format works, it's common for a parser to need to read until the end of
[77] Fix | Delete
# the file to be sure a field is invalid. This consumes a lot of time and memory.
[78] Fix | Delete
#
[79] Fix | Delete
# Luckily, when working with invalid CSV, Ruby's built-in methods will almost
[80] Fix | Delete
# always be superior in every way. For example, parsing non-quoted fields is as
[81] Fix | Delete
# easy as:
[82] Fix | Delete
#
[83] Fix | Delete
# data.split(",")
[84] Fix | Delete
#
[85] Fix | Delete
# == Questions and/or Comments
[86] Fix | Delete
#
[87] Fix | Delete
# Feel free to email {James Edward Gray II}[mailto:james@grayproductions.net]
[88] Fix | Delete
# with any questions.
[89] Fix | Delete
[90] Fix | Delete
require "forwardable"
[91] Fix | Delete
require "date"
[92] Fix | Delete
require "stringio"
[93] Fix | Delete
[94] Fix | Delete
require_relative "csv/fields_converter"
[95] Fix | Delete
require_relative "csv/input_record_separator"
[96] Fix | Delete
require_relative "csv/parser"
[97] Fix | Delete
require_relative "csv/row"
[98] Fix | Delete
require_relative "csv/table"
[99] Fix | Delete
require_relative "csv/writer"
[100] Fix | Delete
[101] Fix | Delete
# == \CSV
[102] Fix | Delete
#
[103] Fix | Delete
# === In a Hurry?
[104] Fix | Delete
#
[105] Fix | Delete
# If you are familiar with \CSV data and have a particular task in mind,
[106] Fix | Delete
# you may want to go directly to the:
[107] Fix | Delete
# - {Recipes for CSV}[doc/csv/recipes/recipes_rdoc.html].
[108] Fix | Delete
#
[109] Fix | Delete
# Otherwise, read on here, about the API: classes, methods, and constants.
[110] Fix | Delete
#
[111] Fix | Delete
# === \CSV Data
[112] Fix | Delete
#
[113] Fix | Delete
# \CSV (comma-separated values) data is a text representation of a table:
[114] Fix | Delete
# - A _row_ _separator_ delimits table rows.
[115] Fix | Delete
# A common row separator is the newline character <tt>"\n"</tt>.
[116] Fix | Delete
# - A _column_ _separator_ delimits fields in a row.
[117] Fix | Delete
# A common column separator is the comma character <tt>","</tt>.
[118] Fix | Delete
#
[119] Fix | Delete
# This \CSV \String, with row separator <tt>"\n"</tt>
[120] Fix | Delete
# and column separator <tt>","</tt>,
[121] Fix | Delete
# has three rows and two columns:
[122] Fix | Delete
# "foo,0\nbar,1\nbaz,2\n"
[123] Fix | Delete
#
[124] Fix | Delete
# Despite the name \CSV, a \CSV representation can use different separators.
[125] Fix | Delete
#
[126] Fix | Delete
# For more about tables, see the Wikipedia article
[127] Fix | Delete
# "{Table (information)}[https://en.wikipedia.org/wiki/Table_(information)]",
[128] Fix | Delete
# especially its section
[129] Fix | Delete
# "{Simple table}[https://en.wikipedia.org/wiki/Table_(information)#Simple_table]"
[130] Fix | Delete
#
[131] Fix | Delete
# == \Class \CSV
[132] Fix | Delete
#
[133] Fix | Delete
# Class \CSV provides methods for:
[134] Fix | Delete
# - Parsing \CSV data from a \String object, a \File (via its file path), or an \IO object.
[135] Fix | Delete
# - Generating \CSV data to a \String object.
[136] Fix | Delete
#
[137] Fix | Delete
# To make \CSV available:
[138] Fix | Delete
# require 'csv'
[139] Fix | Delete
#
[140] Fix | Delete
# All examples here assume that this has been done.
[141] Fix | Delete
#
[142] Fix | Delete
# == Keeping It Simple
[143] Fix | Delete
#
[144] Fix | Delete
# A \CSV object has dozens of instance methods that offer fine-grained control
[145] Fix | Delete
# of parsing and generating \CSV data.
[146] Fix | Delete
# For many needs, though, simpler approaches will do.
[147] Fix | Delete
#
[148] Fix | Delete
# This section summarizes the singleton methods in \CSV
[149] Fix | Delete
# that allow you to parse and generate without explicitly
[150] Fix | Delete
# creating \CSV objects.
[151] Fix | Delete
# For details, follow the links.
[152] Fix | Delete
#
[153] Fix | Delete
# === Simple Parsing
[154] Fix | Delete
#
[155] Fix | Delete
# Parsing methods commonly return either of:
[156] Fix | Delete
# - An \Array of Arrays of Strings:
[157] Fix | Delete
# - The outer \Array is the entire "table".
[158] Fix | Delete
# - Each inner \Array is a row.
[159] Fix | Delete
# - Each \String is a field.
[160] Fix | Delete
# - A CSV::Table object. For details, see
[161] Fix | Delete
# {\CSV with Headers}[#class-CSV-label-CSV+with+Headers].
[162] Fix | Delete
#
[163] Fix | Delete
# ==== Parsing a \String
[164] Fix | Delete
#
[165] Fix | Delete
# The input to be parsed can be a string:
[166] Fix | Delete
# string = "foo,0\nbar,1\nbaz,2\n"
[167] Fix | Delete
#
[168] Fix | Delete
# \Method CSV.parse returns the entire \CSV data:
[169] Fix | Delete
# CSV.parse(string) # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
[170] Fix | Delete
#
[171] Fix | Delete
# \Method CSV.parse_line returns only the first row:
[172] Fix | Delete
# CSV.parse_line(string) # => ["foo", "0"]
[173] Fix | Delete
#
[174] Fix | Delete
# \CSV extends class \String with instance method String#parse_csv,
[175] Fix | Delete
# which also returns only the first row:
[176] Fix | Delete
# string.parse_csv # => ["foo", "0"]
[177] Fix | Delete
#
[178] Fix | Delete
# ==== Parsing Via a \File Path
[179] Fix | Delete
#
[180] Fix | Delete
# The input to be parsed can be in a file:
[181] Fix | Delete
# string = "foo,0\nbar,1\nbaz,2\n"
[182] Fix | Delete
# path = 't.csv'
[183] Fix | Delete
# File.write(path, string)
[184] Fix | Delete
#
[185] Fix | Delete
# \Method CSV.read returns the entire \CSV data:
[186] Fix | Delete
# CSV.read(path) # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
[187] Fix | Delete
#
[188] Fix | Delete
# \Method CSV.foreach iterates, passing each row to the given block:
[189] Fix | Delete
# CSV.foreach(path) do |row|
[190] Fix | Delete
# p row
[191] Fix | Delete
# end
[192] Fix | Delete
# Output:
[193] Fix | Delete
# ["foo", "0"]
[194] Fix | Delete
# ["bar", "1"]
[195] Fix | Delete
# ["baz", "2"]
[196] Fix | Delete
#
[197] Fix | Delete
# \Method CSV.table returns the entire \CSV data as a CSV::Table object:
[198] Fix | Delete
# CSV.table(path) # => #<CSV::Table mode:col_or_row row_count:3>
[199] Fix | Delete
#
[200] Fix | Delete
# ==== Parsing from an Open \IO Stream
[201] Fix | Delete
#
[202] Fix | Delete
# The input to be parsed can be in an open \IO stream:
[203] Fix | Delete
#
[204] Fix | Delete
# \Method CSV.read returns the entire \CSV data:
[205] Fix | Delete
# File.open(path) do |file|
[206] Fix | Delete
# CSV.read(file)
[207] Fix | Delete
# end # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
[208] Fix | Delete
#
[209] Fix | Delete
# As does method CSV.parse:
[210] Fix | Delete
# File.open(path) do |file|
[211] Fix | Delete
# CSV.parse(file)
[212] Fix | Delete
# end # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
[213] Fix | Delete
#
[214] Fix | Delete
# \Method CSV.parse_line returns only the first row:
[215] Fix | Delete
# File.open(path) do |file|
[216] Fix | Delete
# CSV.parse_line(file)
[217] Fix | Delete
# end # => ["foo", "0"]
[218] Fix | Delete
#
[219] Fix | Delete
# \Method CSV.foreach iterates, passing each row to the given block:
[220] Fix | Delete
# File.open(path) do |file|
[221] Fix | Delete
# CSV.foreach(file) do |row|
[222] Fix | Delete
# p row
[223] Fix | Delete
# end
[224] Fix | Delete
# end
[225] Fix | Delete
# Output:
[226] Fix | Delete
# ["foo", "0"]
[227] Fix | Delete
# ["bar", "1"]
[228] Fix | Delete
# ["baz", "2"]
[229] Fix | Delete
#
[230] Fix | Delete
# \Method CSV.table returns the entire \CSV data as a CSV::Table object:
[231] Fix | Delete
# File.open(path) do |file|
[232] Fix | Delete
# CSV.table(file)
[233] Fix | Delete
# end # => #<CSV::Table mode:col_or_row row_count:3>
[234] Fix | Delete
#
[235] Fix | Delete
# === Simple Generating
[236] Fix | Delete
#
[237] Fix | Delete
# \Method CSV.generate returns a \String;
[238] Fix | Delete
# this example uses method CSV#<< to append the rows
[239] Fix | Delete
# that are to be generated:
[240] Fix | Delete
# output_string = CSV.generate do |csv|
[241] Fix | Delete
# csv << ['foo', 0]
[242] Fix | Delete
# csv << ['bar', 1]
[243] Fix | Delete
# csv << ['baz', 2]
[244] Fix | Delete
# end
[245] Fix | Delete
# output_string # => "foo,0\nbar,1\nbaz,2\n"
[246] Fix | Delete
#
[247] Fix | Delete
# \Method CSV.generate_line returns a \String containing the single row
[248] Fix | Delete
# constructed from an \Array:
[249] Fix | Delete
# CSV.generate_line(['foo', '0']) # => "foo,0\n"
[250] Fix | Delete
#
[251] Fix | Delete
# \CSV extends class \Array with instance method <tt>Array#to_csv</tt>,
[252] Fix | Delete
# which forms an \Array into a \String:
[253] Fix | Delete
# ['foo', '0'].to_csv # => "foo,0\n"
[254] Fix | Delete
#
[255] Fix | Delete
# === "Filtering" \CSV
[256] Fix | Delete
#
[257] Fix | Delete
# \Method CSV.filter provides a Unix-style filter for \CSV data.
[258] Fix | Delete
# The input data is processed to form the output data:
[259] Fix | Delete
# in_string = "foo,0\nbar,1\nbaz,2\n"
[260] Fix | Delete
# out_string = ''
[261] Fix | Delete
# CSV.filter(in_string, out_string) do |row|
[262] Fix | Delete
# row[0] = row[0].upcase
[263] Fix | Delete
# row[1] *= 4
[264] Fix | Delete
# end
[265] Fix | Delete
# out_string # => "FOO,0000\nBAR,1111\nBAZ,2222\n"
[266] Fix | Delete
#
[267] Fix | Delete
# == \CSV Objects
[268] Fix | Delete
#
[269] Fix | Delete
# There are three ways to create a \CSV object:
[270] Fix | Delete
# - \Method CSV.new returns a new \CSV object.
[271] Fix | Delete
# - \Method CSV.instance returns a new or cached \CSV object.
[272] Fix | Delete
# - \Method \CSV() also returns a new or cached \CSV object.
[273] Fix | Delete
#
[274] Fix | Delete
# === Instance Methods
[275] Fix | Delete
#
[276] Fix | Delete
# \CSV has three groups of instance methods:
[277] Fix | Delete
# - Its own internally defined instance methods.
[278] Fix | Delete
# - Methods included by module Enumerable.
[279] Fix | Delete
# - Methods delegated to class IO. See below.
[280] Fix | Delete
#
[281] Fix | Delete
# ==== Delegated Methods
[282] Fix | Delete
#
[283] Fix | Delete
# For convenience, a CSV object will delegate to many methods in class IO.
[284] Fix | Delete
# (A few have wrapper "guard code" in \CSV.) You may call:
[285] Fix | Delete
# * IO#binmode
[286] Fix | Delete
# * #binmode?
[287] Fix | Delete
# * IO#close
[288] Fix | Delete
# * IO#close_read
[289] Fix | Delete
# * IO#close_write
[290] Fix | Delete
# * IO#closed?
[291] Fix | Delete
# * #eof
[292] Fix | Delete
# * #eof?
[293] Fix | Delete
# * IO#external_encoding
[294] Fix | Delete
# * IO#fcntl
[295] Fix | Delete
# * IO#fileno
[296] Fix | Delete
# * #flock
[297] Fix | Delete
# * IO#flush
[298] Fix | Delete
# * IO#fsync
[299] Fix | Delete
# * IO#internal_encoding
[300] Fix | Delete
# * #ioctl
[301] Fix | Delete
# * IO#isatty
[302] Fix | Delete
# * #path
[303] Fix | Delete
# * IO#pid
[304] Fix | Delete
# * IO#pos
[305] Fix | Delete
# * IO#pos=
[306] Fix | Delete
# * IO#reopen
[307] Fix | Delete
# * #rewind
[308] Fix | Delete
# * IO#seek
[309] Fix | Delete
# * #stat
[310] Fix | Delete
# * IO#string
[311] Fix | Delete
# * IO#sync
[312] Fix | Delete
# * IO#sync=
[313] Fix | Delete
# * IO#tell
[314] Fix | Delete
# * #to_i
[315] Fix | Delete
# * #to_io
[316] Fix | Delete
# * IO#truncate
[317] Fix | Delete
# * IO#tty?
[318] Fix | Delete
#
[319] Fix | Delete
# === Options
[320] Fix | Delete
#
[321] Fix | Delete
# The default values for options are:
[322] Fix | Delete
# DEFAULT_OPTIONS = {
[323] Fix | Delete
# # For both parsing and generating.
[324] Fix | Delete
# col_sep: ",",
[325] Fix | Delete
# row_sep: :auto,
[326] Fix | Delete
# quote_char: '"',
[327] Fix | Delete
# # For parsing.
[328] Fix | Delete
# field_size_limit: nil,
[329] Fix | Delete
# converters: nil,
[330] Fix | Delete
# unconverted_fields: nil,
[331] Fix | Delete
# headers: false,
[332] Fix | Delete
# return_headers: false,
[333] Fix | Delete
# header_converters: nil,
[334] Fix | Delete
# skip_blanks: false,
[335] Fix | Delete
# skip_lines: nil,
[336] Fix | Delete
# liberal_parsing: false,
[337] Fix | Delete
# nil_value: nil,
[338] Fix | Delete
# empty_value: "",
[339] Fix | Delete
# strip: false,
[340] Fix | Delete
# # For generating.
[341] Fix | Delete
# write_headers: nil,
[342] Fix | Delete
# quote_empty: true,
[343] Fix | Delete
# force_quotes: false,
[344] Fix | Delete
# write_converters: nil,
[345] Fix | Delete
# write_nil_value: nil,
[346] Fix | Delete
# write_empty_value: "",
[347] Fix | Delete
# }
[348] Fix | Delete
#
[349] Fix | Delete
# ==== Options for Parsing
[350] Fix | Delete
#
[351] Fix | Delete
# Options for parsing, described in detail below, include:
[352] Fix | Delete
# - +row_sep+: Specifies the row separator; used to delimit rows.
[353] Fix | Delete
# - +col_sep+: Specifies the column separator; used to delimit fields.
[354] Fix | Delete
# - +quote_char+: Specifies the quote character; used to quote fields.
[355] Fix | Delete
# - +field_size_limit+: Specifies the maximum field size + 1 allowed.
[356] Fix | Delete
# Deprecated since 3.2.3. Use +max_field_size+ instead.
[357] Fix | Delete
# - +max_field_size+: Specifies the maximum field size allowed.
[358] Fix | Delete
# - +converters+: Specifies the field converters to be used.
[359] Fix | Delete
# - +unconverted_fields+: Specifies whether unconverted fields are to be available.
[360] Fix | Delete
# - +headers+: Specifies whether data contains headers,
[361] Fix | Delete
# or specifies the headers themselves.
[362] Fix | Delete
# - +return_headers+: Specifies whether headers are to be returned.
[363] Fix | Delete
# - +header_converters+: Specifies the header converters to be used.
[364] Fix | Delete
# - +skip_blanks+: Specifies whether blanks lines are to be ignored.
[365] Fix | Delete
# - +skip_lines+: Specifies how comments lines are to be recognized.
[366] Fix | Delete
# - +strip+: Specifies whether leading and trailing whitespace are to be
[367] Fix | Delete
# stripped from fields. This must be compatible with +col_sep+; if it is not,
[368] Fix | Delete
# then an +ArgumentError+ exception will be raised.
[369] Fix | Delete
# - +liberal_parsing+: Specifies whether \CSV should attempt to parse
[370] Fix | Delete
# non-compliant data.
[371] Fix | Delete
# - +nil_value+: Specifies the object that is to be substituted for each null (no-text) field.
[372] Fix | Delete
# - +empty_value+: Specifies the object that is to be substituted for each empty field.
[373] Fix | Delete
#
[374] Fix | Delete
# :include: ../doc/csv/options/common/row_sep.rdoc
[375] Fix | Delete
#
[376] Fix | Delete
# :include: ../doc/csv/options/common/col_sep.rdoc
[377] Fix | Delete
#
[378] Fix | Delete
# :include: ../doc/csv/options/common/quote_char.rdoc
[379] Fix | Delete
#
[380] Fix | Delete
# :include: ../doc/csv/options/parsing/field_size_limit.rdoc
[381] Fix | Delete
#
[382] Fix | Delete
# :include: ../doc/csv/options/parsing/converters.rdoc
[383] Fix | Delete
#
[384] Fix | Delete
# :include: ../doc/csv/options/parsing/unconverted_fields.rdoc
[385] Fix | Delete
#
[386] Fix | Delete
# :include: ../doc/csv/options/parsing/headers.rdoc
[387] Fix | Delete
#
[388] Fix | Delete
# :include: ../doc/csv/options/parsing/return_headers.rdoc
[389] Fix | Delete
#
[390] Fix | Delete
# :include: ../doc/csv/options/parsing/header_converters.rdoc
[391] Fix | Delete
#
[392] Fix | Delete
# :include: ../doc/csv/options/parsing/skip_blanks.rdoc
[393] Fix | Delete
#
[394] Fix | Delete
# :include: ../doc/csv/options/parsing/skip_lines.rdoc
[395] Fix | Delete
#
[396] Fix | Delete
# :include: ../doc/csv/options/parsing/strip.rdoc
[397] Fix | Delete
#
[398] Fix | Delete
# :include: ../doc/csv/options/parsing/liberal_parsing.rdoc
[399] Fix | Delete
#
[400] Fix | Delete
# :include: ../doc/csv/options/parsing/nil_value.rdoc
[401] Fix | Delete
#
[402] Fix | Delete
# :include: ../doc/csv/options/parsing/empty_value.rdoc
[403] Fix | Delete
#
[404] Fix | Delete
# ==== Options for Generating
[405] Fix | Delete
#
[406] Fix | Delete
# Options for generating, described in detail below, include:
[407] Fix | Delete
# - +row_sep+: Specifies the row separator; used to delimit rows.
[408] Fix | Delete
# - +col_sep+: Specifies the column separator; used to delimit fields.
[409] Fix | Delete
# - +quote_char+: Specifies the quote character; used to quote fields.
[410] Fix | Delete
# - +write_headers+: Specifies whether headers are to be written.
[411] Fix | Delete
# - +force_quotes+: Specifies whether each output field is to be quoted.
[412] Fix | Delete
# - +quote_empty+: Specifies whether each empty output field is to be quoted.
[413] Fix | Delete
# - +write_converters+: Specifies the field converters to be used in writing.
[414] Fix | Delete
# - +write_nil_value+: Specifies the object that is to be substituted for each +nil+-valued field.
[415] Fix | Delete
# - +write_empty_value+: Specifies the object that is to be substituted for each empty field.
[416] Fix | Delete
#
[417] Fix | Delete
# :include: ../doc/csv/options/common/row_sep.rdoc
[418] Fix | Delete
#
[419] Fix | Delete
# :include: ../doc/csv/options/common/col_sep.rdoc
[420] Fix | Delete
#
[421] Fix | Delete
# :include: ../doc/csv/options/common/quote_char.rdoc
[422] Fix | Delete
#
[423] Fix | Delete
# :include: ../doc/csv/options/generating/write_headers.rdoc
[424] Fix | Delete
#
[425] Fix | Delete
# :include: ../doc/csv/options/generating/force_quotes.rdoc
[426] Fix | Delete
#
[427] Fix | Delete
# :include: ../doc/csv/options/generating/quote_empty.rdoc
[428] Fix | Delete
#
[429] Fix | Delete
# :include: ../doc/csv/options/generating/write_converters.rdoc
[430] Fix | Delete
#
[431] Fix | Delete
# :include: ../doc/csv/options/generating/write_nil_value.rdoc
[432] Fix | Delete
#
[433] Fix | Delete
# :include: ../doc/csv/options/generating/write_empty_value.rdoc
[434] Fix | Delete
#
[435] Fix | Delete
# === \CSV with Headers
[436] Fix | Delete
#
[437] Fix | Delete
# CSV allows to specify column names of CSV file, whether they are in data, or
[438] Fix | Delete
# provided separately. If headers are specified, reading methods return an instance
[439] Fix | Delete
# of CSV::Table, consisting of CSV::Row.
[440] Fix | Delete
#
[441] Fix | Delete
# # Headers are part of data
[442] Fix | Delete
# data = CSV.parse(<<~ROWS, headers: true)
[443] Fix | Delete
# Name,Department,Salary
[444] Fix | Delete
# Bob,Engineering,1000
[445] Fix | Delete
# Jane,Sales,2000
[446] Fix | Delete
# John,Management,5000
[447] Fix | Delete
# ROWS
[448] Fix | Delete
#
[449] Fix | Delete
# data.class #=> CSV::Table
[450] Fix | Delete
# data.first #=> #<CSV::Row "Name":"Bob" "Department":"Engineering" "Salary":"1000">
[451] Fix | Delete
# data.first.to_h #=> {"Name"=>"Bob", "Department"=>"Engineering", "Salary"=>"1000"}
[452] Fix | Delete
#
[453] Fix | Delete
# # Headers provided by developer
[454] Fix | Delete
# data = CSV.parse('Bob,Engineering,1000', headers: %i[name department salary])
[455] Fix | Delete
# data.first #=> #<CSV::Row name:"Bob" department:"Engineering" salary:"1000">
[456] Fix | Delete
#
[457] Fix | Delete
# === \Converters
[458] Fix | Delete
#
[459] Fix | Delete
# By default, each value (field or header) parsed by \CSV is formed into a \String.
[460] Fix | Delete
# You can use a _field_ _converter_ or _header_ _converter_
[461] Fix | Delete
# to intercept and modify the parsed values:
[462] Fix | Delete
# - See {Field Converters}[#class-CSV-label-Field+Converters].
[463] Fix | Delete
# - See {Header Converters}[#class-CSV-label-Header+Converters].
[464] Fix | Delete
#
[465] Fix | Delete
# Also by default, each value to be written during generation is written 'as-is'.
[466] Fix | Delete
# You can use a _write_ _converter_ to modify values before writing.
[467] Fix | Delete
# - See {Write Converters}[#class-CSV-label-Write+Converters].
[468] Fix | Delete
#
[469] Fix | Delete
# ==== Specifying \Converters
[470] Fix | Delete
#
[471] Fix | Delete
# You can specify converters for parsing or generating in the +options+
[472] Fix | Delete
# argument to various \CSV methods:
[473] Fix | Delete
# - Option +converters+ for converting parsed field values.
[474] Fix | Delete
# - Option +header_converters+ for converting parsed header values.
[475] Fix | Delete
# - Option +write_converters+ for converting values to be written (generated).
[476] Fix | Delete
#
[477] Fix | Delete
# There are three forms for specifying converters:
[478] Fix | Delete
# - A converter proc: executable code to be used for conversion.
[479] Fix | Delete
# - A converter name: the name of a stored converter.
[480] Fix | Delete
# - A converter list: an array of converter procs, converter names, and converter lists.
[481] Fix | Delete
#
[482] Fix | Delete
# ===== Converter Procs
[483] Fix | Delete
#
[484] Fix | Delete
# This converter proc, +strip_converter+, accepts a value +field+
[485] Fix | Delete
# and returns <tt>field.strip</tt>:
[486] Fix | Delete
# strip_converter = proc {|field| field.strip }
[487] Fix | Delete
# In this call to <tt>CSV.parse</tt>,
[488] Fix | Delete
# the keyword argument <tt>converters: string_converter</tt>
[489] Fix | Delete
# specifies that:
[490] Fix | Delete
# - \Proc +string_converter+ is to be called for each parsed field.
[491] Fix | Delete
# - The converter's return value is to replace the +field+ value.
[492] Fix | Delete
# Example:
[493] Fix | Delete
# string = " foo , 0 \n bar , 1 \n baz , 2 \n"
[494] Fix | Delete
# array = CSV.parse(string, converters: strip_converter)
[495] Fix | Delete
# array # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
[496] Fix | Delete
#
[497] Fix | Delete
# A converter proc can receive a second argument, +field_info+,
[498] Fix | Delete
# that contains details about the field.
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function