This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
====== Option +force_quotes+
|
||||
|
||||
Specifies the boolean that determines whether each output field is to be double-quoted.
|
||||
|
||||
Default value:
|
||||
CSV::DEFAULT_OPTIONS.fetch(:force_quotes) # => false
|
||||
|
||||
For examples in this section:
|
||||
ary = ['foo', 0, nil]
|
||||
|
||||
Using the default, +false+:
|
||||
str = CSV.generate_line(ary)
|
||||
str # => "foo,0,\n"
|
||||
|
||||
Using +true+:
|
||||
str = CSV.generate_line(ary, force_quotes: true)
|
||||
str # => "\"foo\",\"0\",\"\"\n"
|
||||
@@ -0,0 +1,12 @@
|
||||
====== Option +quote_empty+
|
||||
|
||||
Specifies the boolean that determines whether an empty value is to be double-quoted.
|
||||
|
||||
Default value:
|
||||
CSV::DEFAULT_OPTIONS.fetch(:quote_empty) # => true
|
||||
|
||||
With the default +true+:
|
||||
CSV.generate_line(['"', ""]) # => "\"\"\"\",\"\"\n"
|
||||
|
||||
With +false+:
|
||||
CSV.generate_line(['"', ""], quote_empty: false) # => "\"\"\"\",\n"
|
||||
@@ -0,0 +1,25 @@
|
||||
====== Option +write_converters+
|
||||
|
||||
Specifies converters to be used in generating fields.
|
||||
See {Write Converters}[#class-CSV-label-Write+Converters]
|
||||
|
||||
Default value:
|
||||
CSV::DEFAULT_OPTIONS.fetch(:write_converters) # => nil
|
||||
|
||||
With no write converter:
|
||||
str = CSV.generate_line(["\na\n", "\tb\t", " c "])
|
||||
str # => "\"\na\n\",\tb\t, c \n"
|
||||
|
||||
With a write converter:
|
||||
strip_converter = proc {|field| field.strip }
|
||||
str = CSV.generate_line(["\na\n", "\tb\t", " c "], write_converters: strip_converter)
|
||||
str # => "a,b,c\n"
|
||||
|
||||
With two write converters (called in order):
|
||||
upcase_converter = proc {|field| field.upcase }
|
||||
downcase_converter = proc {|field| field.downcase }
|
||||
write_converters = [upcase_converter, downcase_converter]
|
||||
str = CSV.generate_line(['a', 'b', 'c'], write_converters: write_converters)
|
||||
str # => "a,b,c\n"
|
||||
|
||||
See also {Write Converters}[#class-CSV-label-Write+Converters]
|
||||
@@ -0,0 +1,15 @@
|
||||
====== Option +write_empty_value+
|
||||
|
||||
Specifies the object that is to be substituted for each field
|
||||
that has an empty \String.
|
||||
|
||||
Default value:
|
||||
CSV::DEFAULT_OPTIONS.fetch(:write_empty_value) # => ""
|
||||
|
||||
Without the option:
|
||||
str = CSV.generate_line(['a', '', 'c', ''])
|
||||
str # => "a,\"\",c,\"\"\n"
|
||||
|
||||
With the option:
|
||||
str = CSV.generate_line(['a', '', 'c', ''], write_empty_value: "x")
|
||||
str # => "a,x,c,x\n"
|
||||
@@ -0,0 +1,29 @@
|
||||
====== Option +write_headers+
|
||||
|
||||
Specifies the boolean that determines whether a header row is included in the output;
|
||||
ignored if there are no headers.
|
||||
|
||||
Default value:
|
||||
CSV::DEFAULT_OPTIONS.fetch(:write_headers) # => nil
|
||||
|
||||
Without +write_headers+:
|
||||
file_path = 't.csv'
|
||||
CSV.open(file_path,'w',
|
||||
:headers => ['Name','Value']
|
||||
) do |csv|
|
||||
csv << ['foo', '0']
|
||||
end
|
||||
CSV.open(file_path) do |csv|
|
||||
csv.shift
|
||||
end # => ["foo", "0"]
|
||||
|
||||
With +write_headers+":
|
||||
CSV.open(file_path,'w',
|
||||
:write_headers => true,
|
||||
:headers => ['Name','Value']
|
||||
) do |csv|
|
||||
csv << ['foo', '0']
|
||||
end
|
||||
CSV.open(file_path) do |csv|
|
||||
csv.shift
|
||||
end # => ["Name", "Value"]
|
||||
@@ -0,0 +1,14 @@
|
||||
====== Option +write_nil_value+
|
||||
|
||||
Specifies the object that is to be substituted for each +nil+-valued field.
|
||||
|
||||
Default value:
|
||||
CSV::DEFAULT_OPTIONS.fetch(:write_nil_value) # => nil
|
||||
|
||||
Without the option:
|
||||
str = CSV.generate_line(['a', nil, 'c', nil])
|
||||
str # => "a,,c,\n"
|
||||
|
||||
With the option:
|
||||
str = CSV.generate_line(['a', nil, 'c', nil], write_nil_value: "x")
|
||||
str # => "a,x,c,x\n"
|
||||
Reference in New Issue
Block a user