init_separators(options)
private
Stores the indicated separators for later use.
If auto-discovery was requested for @row_sep, this method will read ahead in the @io and try to find
one. ARGF, STDIN, STDOUT, STDERR and
any stream open for output only with a
default @row_sep of $INPUT_RECORD_SEPARATOR
($/).
This method also establishes the quoting rules used for CSV output.
Show source
def init_separators(options)
@col_sep = options.delete(:col_sep).to_s.encode(@encoding)
@row_sep = options.delete(:row_sep)
@quote_char = options.delete(:quote_char).to_s.encode(@encoding)
if @quote_char.length != 1
raise ArgumentError, ":quote_char has to be a single character String"
end
if @row_sep == :auto
if [ARGF, STDIN, STDOUT, STDERR].include?(@io) or
(defined?(Zlib) and @io.class == Zlib::GzipWriter)
@row_sep = $INPUT_RECORD_SEPARATOR
else
begin
saved_pos = @io.pos
while @row_sep == :auto
if @io.eof?
@row_sep = $INPUT_RECORD_SEPARATOR
break
end
sample = read_to_char(1024)
sample += read_to_char(1) if sample[-1..-1] == encode_str("\r") and
not @io.eof?
if sample =~ encode_re("\r\n?|\n")
@row_sep = $&
break
end
end
@io.rewind
while saved_pos > 1024
@io.read(1024)
saved_pos -= 1024
end
@io.read(saved_pos) if saved_pos.nonzero?
rescue IOError
@row_sep = $INPUT_RECORD_SEPARATOR
end
end
end
@row_sep = @row_sep.to_s.encode(@encoding)
@force_quotes = options.delete(:force_quotes)
do_quote = lambda do |field|
@quote_char +
String(field).gsub(@quote_char, @quote_char * 2) +
@quote_char
end
quotable_chars = encode_str("\r\n", @col_sep, @quote_char)
@quote = if @force_quotes
do_quote
else
lambda do |field|
if field.nil?
""
else
field = String(field)
if field.empty? or
field.count(quotable_chars).nonzero?
do_quote.call(field)
else
field
end
end
end
end
end