Class: Uncsv::Rows

Inherits:
Object
  • Object
show all
Defined in:
lib/uncsv/rows.rb

Overview

A collection of parsed rows from a CSV

Instance Method Summary collapse

Constructor Details

#initialize(csv, config = nil) ⇒ Rows

Create a new Rows object

Parameters:

  • csv (CSV)

    A CSV object.

  • config (Config) (defaults to: nil)

    Configuration options. Default options if nil.



13
14
15
16
17
18
# File 'lib/uncsv/rows.rb', line 13

def initialize(csv, config = nil)
  @csv = csv
  @config = config || Config.new
  @started = false
  @parsed = nil
end

Instance Method Details

#each {|row| ... } ⇒ Enumerator

Iterate over each row

Yields:

  • A block to run for each row

Yield Parameters:

  • row (Row)

    A row object

Returns:

  • (Enumerator)

    An enumerator over each row



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/uncsv/rows.rb', line 25

def each(&block)
  Enumerator.new do |yielder|
    start
    index = parsed.index
    loop do
      break unless yield_row(yielder, index)

      index += 1
    end
  end.each(&block)
end

#headerArray

Get the CSV header

Returns:

  • (Array)

    An array of the CSV header fields

See Also:



41
42
43
# File 'lib/uncsv/rows.rb', line 41

def header
  parsed.header.to_a
end