class NesquikCSV
Fast CSV parser using native code
Constants
- VERSION
Public Class Methods
foreach(path, &block)
click to toggle source
Pass each line of the specified path
as array to the provided block
# File lib/nesquikcsv.rb, line 13 def self.foreach(path, &block) open(path) do |reader| reader.each(&block) end end
new(io, encoding="UTF-8")
click to toggle source
Create new NesquikCSV
wrapping the specified IO object
# File lib/nesquikcsv.rb, line 66 def initialize(io, encoding="UTF-8") @io = io @encoding = encoding end
open(path, mode = "rb") { |csv| ... }
click to toggle source
Opens a csv file. Pass a NesquikCSV
instance to the provided block, or return it when no block is provided
# File lib/nesquikcsv.rb, line 21 def self.open(path, mode = "rb") csv = new(File.open(path, mode)) if block_given? begin yield csv ensure csv.close end else csv end end
parse(data, encoding="UTF-8", &block)
click to toggle source
Read all lines from the specified String into an array of arrays
# File lib/nesquikcsv.rb, line 45 def self.parse(data, encoding="UTF-8", &block) csv = new(StringIO.new(data), encoding) if block.nil? begin csv.read ensure csv.close end else csv.each(&block) end end
parse_line(line, encoding="UTF-8")
click to toggle source
# File lib/nesquikcsv.rb, line 58 def self.parse_line(line, encoding="UTF-8") if RUBY_PLATFORM =~ /java/ line = line.force_encoding(encoding) end safe_to_a CsvParser.parse_line(line, encoding) end
read(path)
click to toggle source
Read all lines from the specified path
into an array of arrays
# File lib/nesquikcsv.rb, line 35 def self.read(path) open(path, "rb") { |csv| csv.read } end
readlines(path)
click to toggle source
Alias for read
# File lib/nesquikcsv.rb, line 40 def self.readlines(path) read(path) end
safe_to_a(arg)
click to toggle source
# File lib/nesquikcsv.rb, line 127 def self.safe_to_a(arg) if arg.nil? nil else arg.to_a end end
Public Instance Methods
close()
click to toggle source
Close the wrapped IO
# File lib/nesquikcsv.rb, line 107 def close @io.close end
closed?()
click to toggle source
# File lib/nesquikcsv.rb, line 111 def closed? @io.closed? end
each() { |row| ... }
click to toggle source
Read from the wrapped IO passing each line as array to the specified block
# File lib/nesquikcsv.rb, line 72 def each if block_given? while row = shift(@encoding) yield row end else to_enum # return enumerator end end
get_line_with_quotes()
click to toggle source
# File lib/nesquikcsv.rb, line 115 def get_line_with_quotes line = @io.gets if !line.nil? while line.count('"').odd? next_line = @io.gets break if next_line.nil? line << next_line end end line end
read()
click to toggle source
Read all remaining lines from the wrapped IO into an array of arrays
# File lib/nesquikcsv.rb, line 83 def read table = Array.new each {|row| table << row} table end
Also aliased as: readlines
rewind()
click to toggle source
Rewind the underlying IO object and reset line counter
# File lib/nesquikcsv.rb, line 91 def rewind @io.rewind end
shift(encoding='UTF-8')
click to toggle source
Read next line from the wrapped IO and return as array or nil at EOF
# File lib/nesquikcsv.rb, line 96 def shift(encoding='UTF-8') if line = get_line_with_quotes NesquikCSV.safe_to_a CsvParser.parse_line(line, encoding) else nil end end