CSV::Writer (Class)

In: csv.rb
Parent: Object

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

str_or_writable must handle ’<<(string)’.

[Source]

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

Generate CSV. Given block is called with the writer instance.

[Source]

# File csv.rb, line 672
    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

[Source]

# File csv.rb, line 711
    def initialize(dev)
      raise RuntimeError.new('do not instantiate this class directly')
    end

Public Instance methods

dump CSV stream to the device. argument must be an Array of String.

[Source]

# File csv.rb, line 685
    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

dump CSV stream to the device. argument must be an Array of CSV::Cell.

[Source]

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

[Source]

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

[Validate]