# CSV::Writer (Class)

In: | csv.rb
Parent: | Object

DESCRIPTION

      CSV::Writer -- CSV formatted string/stream writer.

EXAMPLE

      Write rows to 'csvout' file.

      outfile = File.open('csvout', 'wb')
      CSV::Writer.generate(outfile) do |csv|
        csv << ['c1', nil, '', '"', "\r\n", 'c2']
        # or
        csv.add_row [
          CSV::Cell.new('c1', false),
          CSV::Cell.new('dummy', true),
          CSV::Cell.new('', false),
          CSV::Cell.new('"', false),
          CSV::Cell.new("\r\n", false)
          CSV::Cell.new('c2', false)
        ]
        ...
        ...
      end

      outfile.close

## Methods

<< add_row close create generate new

## Public Class methods

create(str_or_writable, col_sep = ?,, row_sep = nil)

SYNOPSIS

      writer = CSV::Writer.create(str_or_readable)

ARGS

      str_or_writable: device for generated CSV string.  Must respond to
        '<<(string)'.

RETURNS

      writer: Created instance.

DESCRIPTION

      Create instance.  To add CSV data to generate CSV string, see
      CSV::Writer#<< or CSV::Writer#add_row.

[Source]

    # File csv.rb, line 555
        def Writer.create(str_or_writable, col_sep = ?,, row_sep = nil)
          BasicWriter.new(str_or_writable, col_sep, row_sep)
        end

generate(str_or_writable, col_sep = ?,, row_sep = nil) {|writer| ...}

SYNOPSIS

      CSV::Writer.generate(str_or_writable) do |writer|
        ...
      end

ARGS

      str_or_writable: device for generated CSV string.  Must respond to
        '<<(string)'.
      writer: Created writer instance.  See CSV::Writer#<< and
        CSV::Writer#add_row to know how to generate CSV string.

RETURNS

      nil

DESCRIPTION

      Create writer instance.  Caller block is called with the new instance.
      To add CSV data to generate CSV string, see CSV::Writer#<< or
      CSV::Writer#add_row.

[Source]

    # File csv.rb, line 578
        def Writer.generate(str_or_writable, col_sep = ?,, row_sep = nil)
          writer = Writer.create(str_or_writable, col_sep, row_sep)
          yield(writer)
          writer.close
          nil
        end

new(dev)

[Source]

    # File csv.rb, line 645
        def initialize(dev)
          raise RuntimeError.new('Do not instanciate this class directly.')
        end

## Public Instance methods

<<(ary)

SYNOPSIS

      CSV::Writer#<<(row)

ARGS

      row: an Array of a String.

RETURNS

      self

DESCRIPTION

      Dump CSV stream to the device.  Argument is an array of a String like
      ['c1', 'c2', 'c3'].

[Source]

    # File csv.rb, line 598
        def <<(ary)
          row = ary.collect { |item|
            if item.is_a?(Cell)
              item
            elsif (item.nil?)
              Cell.new('', true)
            else
              Cell.new(item.to_s, false)
            end
          }
          CSV.generate_row(row, row.size, @dev, @col_sep, @row_sep)
          self
        end

add_row(row)

SYNOPSIS

      CSV::Writer#<<(row)

ARGS

      row: an Array of a CSV::Cell.

RETURNS

      self

DESCRIPTION

      Dump CSV stream to the device.  Argument is an array of a CSV::Cell
      like [CSV::Cell.new('c1', false), CSV::Cell.new('dummy', true)].
      (Formar is 'c1' and latter is Null.)

[Source]

    # File csv.rb, line 626
        def add_row(row)
          CSV.generate_row(row, row.size, @dev, @col_sep, @row_sep)
          self
        end

close()

SYNOPSIS

      CSV::Writer#close

RETURNS

      nil

DESCRIPTION

      Close this writer.

[Source]

    # File csv.rb, line 640
        def close
          terminate
        end

[Validate]