CSV::Reader (Class)

In: csv.rb
Parent: Object

CSV formatted string/stream reader.

EXAMPLE

  read CSV lines until 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

Methods

close   create   each   new   parse   shift  

Included Modules

Enumerable

Public Class methods

Returns reader instance.

[Source]

# File csv.rb, line 524
    def Reader.create(str_or_readable, col_sep = ?,, row_sep = nil)
      case str_or_readable
      when IO
        IOReader.new(str_or_readable, col_sep, row_sep)
      when String
        StringReader.new(str_or_readable, col_sep, row_sep)
      else
        IOReader.new(str_or_readable, col_sep, row_sep)
      end
    end

[Source]

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

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.

[Source]

# File csv.rb, line 514
    def Reader.parse(str_or_readable, col_sep = ?,, row_sep = nil)
      reader = create(str_or_readable, col_sep, row_sep)
      reader.each do |row|
        yield(row)
      end
      reader.close
      nil
    end

Public Instance methods

[Source]

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

[Source]

# File csv.rb, line 535
    def each
      while true
        row = Row.new
        parsed_cells = get_row(row)
        if parsed_cells == 0
          break
        end
        yield(row)
      end
      nil
    end

[Source]

# File csv.rb, line 547
    def shift
      row = Row.new
      parsed_cells = get_row(row)
      row
    end

[Validate]