2020-05-12 17:42:45 -04:00
|
|
|
====== Option +write_converters+
|
|
|
|
|
2020-07-15 16:37:17 -04:00
|
|
|
Specifies converters to be used in generating fields.
|
|
|
|
See {Write Converters}[#class-CSV-label-Write+Converters]
|
2020-05-12 17:42:45 -04:00
|
|
|
|
|
|
|
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:
|
2020-07-15 16:37:17 -04:00
|
|
|
strip_converter = proc {|field| field.strip }
|
2020-05-12 17:42:45 -04:00
|
|
|
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):
|
2020-07-15 16:37:17 -04:00
|
|
|
upcase_converter = proc {|field| field.upcase }
|
|
|
|
downcase_converter = proc {|field| field.downcase }
|
2020-05-12 17:42:45 -04:00
|
|
|
write_converters = [upcase_converter, downcase_converter]
|
|
|
|
str = CSV.generate_line(['a', 'b', 'c'], write_converters: write_converters)
|
|
|
|
str # => "a,b,c\n"
|
|
|
|
|
2020-07-15 16:37:17 -04:00
|
|
|
See also {Write Converters}[#class-CSV-label-Write+Converters]
|
|
|
|
|
2020-05-12 17:42:45 -04:00
|
|
|
---
|
|
|
|
|
|
|
|
Raises an exception if the converter returns a value that is neither +nil+
|
|
|
|
nor \String-convertible:
|
2020-07-15 16:37:17 -04:00
|
|
|
bad_converter = proc {|field| BasicObject.new }
|
2020-05-12 17:42:45 -04:00
|
|
|
# Raises NoMethodError (undefined method `is_a?' for #<BasicObject:>)
|
|
|
|
CSV.generate_line(['a', 'b', 'c'], write_converters: bad_converter)
|