Skip to Content Skip to Search

Note: Don’t use this class directly. This is an internal class.

Methods
#
N
R

Attributes

[R] headers
[R] lineno

A CSV::Writer receives an output, prepares the header, format and output. It allows us to write new rows in the object and rewind it.

Class Public methods

new(output, options)

# File ruby/lib/csv/writer.rb, line 16
def initialize(output, options)
  @output = output
  @options = options
  @lineno = 0
  @fields_converter = nil
  prepare
  if @options[:write_headers] and @headers
    self << @headers
  end
  @fields_converter = @options[:fields_converter]
end

Instance Public methods

<<(row)

Adds a new row

# File ruby/lib/csv/writer.rb, line 31
def <<(row)
  case row
  when Row
    row = row.fields
  when Hash
    row = @headers.collect {|header| row[header]}
  end

  @headers ||= row if @use_headers
  @lineno += 1

  if @fields_converter
    quoted_fields = [false] * row.size
    row = @fields_converter.convert(row, nil, lineno, quoted_fields)
  end

  i = -1
  converted_row = row.collect do |field|
    i += 1
    quote(field, i)
  end
  line = converted_row.join(@column_separator) + @row_separator
  if @output_encoding
    line = line.encode(@output_encoding)
  end
  @output << line

  self
end

rewind()

Winds back to the beginning

# File ruby/lib/csv/writer.rb, line 64
def rewind
  @lineno = 0
  @headers = nil if @options[:headers].nil?
end