In Files

  • csv.rb

Parent

Included Modules

CSV::Reader

CSV formatted string/stream reader.

EXAMPLE

read CSV lines untill the first column is 'stop'.

CSV::Reader.parse(File.open('bigdata', 'rb')) do |row|
  p row
  break if !row[0].is_null && row[0].data == 'stop'
end

Public Class Methods

create(str_or_readable, fs = ',', rs = nil) click to toggle source

Returns reader instance.

 
               # File csv.rb, line 542
def Reader.create(str_or_readable, fs = ',', rs = nil)
  case str_or_readable
  when IO
    IOReader.new(str_or_readable, fs, rs)
  when String
    StringReader.new(str_or_readable, fs, rs)
  else
    IOReader.new(str_or_readable, fs, rs)
  end
end
            
new(dev) click to toggle source
 
               # File csv.rb, line 577
def initialize(dev)
  raise RuntimeError.new('Do not instanciate this class directly.')
end
            
parse(str_or_readable, fs = ',', rs = nil, &block) click to toggle source

Parse CSV data and get lines. Given block is called for each parsed row. Block value is always nil. Rows are not cached for performance reason.

 
               # File csv.rb, line 528
def Reader.parse(str_or_readable, fs = ',', rs = nil, &block)
  reader = Reader.create(str_or_readable, fs, rs)
  if block
    reader.each do |row|
      yield(row)
    end
    reader.close
    nil
  else
    reader
  end
end
            

Public Instance Methods

close() click to toggle source
 
               # File csv.rb, line 571
def close
  terminate
end
            
each() click to toggle source
 
               # File csv.rb, line 553
def each
  while true
    row = []
    parsed_cells = get_row(row)
    if parsed_cells == 0
      break
    end
    yield(row)
  end
  nil
end
            
shift() click to toggle source
 
               # File csv.rb, line 565
def shift
  row = []
  parsed_cells = get_row(row)
  row
end
            

Commenting is here to help enhance the documentation. For example, code samples, or clarification of the documentation.

If you have questions about Ruby or the documentation, please post to one of the Ruby mailing lists. You will get better, faster, help that way.

If you wish to post a correction of the docs, please do so, but also file bug report so that it can be corrected for the next release. Thank you.

If you want to help improve the Ruby documentation, please visit Documenting-ruby.org.

blog comments powered by Disqus