Class: CSV::Writer (Ruby 2.7.2)
Description: Class : CSV::Writer - Ruby 2.7.2

### Home Classes Methods

### In Files

- csv/writer.rb

### Parent

Object

### Methods

- ::new
- #<<
- #rewind

### Class/Module Index

Quicksearch

- CSV
- CSV::DeleteSuffix
- CSV::FieldsConverter
- CSV::MalformedCSVError
- CSV::MatchP
- CSV::Parser
- CSV::Parser::InputsScanner
- CSV::Parser::InvalidEncoding
- CSV::Parser::Scanner
- CSV::Parser::UnoptimizedStringIO
- CSV::Row
- CSV::Table
- CSV::Writer
- Object

No matching classes.

# CSV::Writer

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

### Attributes

headers[R]
lineno[R]

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.

### Public Class Methods

new(output, options) click to toggle source

                   # File csv/writer.rb, line 18
    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

### Public Instance Methods

<<(row) click to toggle source

Adds a new row

                   # File csv/writer.rb, line 33
    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

      row = @fields_converter.convert(row, nil, lineno) if @fields_converter

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

      self
    end

rewind() click to toggle source

Winds back to the beginning

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