Class: Tabula::Writers::CSVWriter
- Defined in:
- lib/tabula/writers/csv_writer.rb
Overview
Writes tables in CSV format
Class Method Summary collapse
-
.to_string(tables, **options) ⇒ String
Write tables to a string.
Instance Method Summary collapse
-
#initialize(separator: ',', quote_char: '"', force_quotes: false, **options) ⇒ CSVWriter
constructor
A new instance of CSVWriter.
-
#write(tables, io) ⇒ Object
Write tables to an IO object.
Methods inherited from Writer
Constructor Details
#initialize(separator: ',', quote_char: '"', force_quotes: false, **options) ⇒ CSVWriter
Returns a new instance of CSVWriter.
12 13 14 15 16 17 |
# File 'lib/tabula/writers/csv_writer.rb', line 12 def initialize(separator: ',', quote_char: '"', force_quotes: false, **) super(**) @separator = separator @quote_char = quote_char @force_quotes = force_quotes end |
Class Method Details
.to_string(tables, **options) ⇒ String
Write tables to a string
41 42 43 44 45 46 |
# File 'lib/tabula/writers/csv_writer.rb', line 41 def self.to_string(tables, **) require 'stringio' io = StringIO.new new(**).write(tables, io) io.string end |
Instance Method Details
#write(tables, io) ⇒ Object
Write tables to an IO object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/tabula/writers/csv_writer.rb', line 22 def write(tables, io) = { col_sep: @separator, quote_char: @quote_char, force_quotes: @force_quotes } tables.each_with_index do |table, idx| # Add blank line between tables io.puts if idx.positive? csv = CSV.new(io, **) table.to_a.each { |row| csv << row } end end |