Class: FasterCSV::Row

Class | FasterCSV::Row
In: | lib/faster_csv.rb
Parent: | Object

A FasterCSV::Row is part Array and part Hash. It retains an order for the fields and allows duplicates just as an Array would, but also allows you to access fields by name just as you could if they were in a Hash.

All rows returned by FasterCSV will be constructed from this class, if header row processing is activated.

### Methods

<< == [] []= delete delete_if each field field? field_row? fields header? header_row? headers include? index inspect new push to_csv to_hash to_s values_at

### Included Modules

Enumerable

### Attributes

row | [R] | Internal data format used to compare equality.

### Public Class methods

new(headers, fields, header_row = false)

Construct a new FasterCSV::Row from `headers` and `fields`, which are expected to be Arrays. If one Array is shorter than the other, it will be padded with `nil` objects.

The optional `header_row` parameter can be set to `true` to indicate, via FasterCSV::Row.header_row?() and FasterCSV::Row.field_row?(), that this is a header row. Otherwise, the row is assumes to be a field row.

A FasterCSV::Row object supports the following Array methods through delegation:

- empty?()
- length()
- size()

### Public Instance methods

<<( field )
<<( header_and_field_array )
<<( header_and_field_hash )

If a two-element Array is provided, it is assumed to be a header and field and the pair is appended. A Hash works the same way with the key being the header and the value being the field. Anything else is assumed to be a lone field which is appended with a `nil` header.

This method returns the row for chaining.

==(other)

Returns `true` if this row contains the same headers and fields in the same order as `other`.

[](header_or_index, minimum_index = 0)

Alias for field

[]=( header, value )
[]=( header, offset, value )
[]=( index, value )

Looks up the field by the semantics described in FasterCSV::Row.field() and assigns the `value`.

Assigning past the end of the row with an index will set all pairs between to `[nil, nil]`. Assigning to an unused header appends the new pair.

delete( header )
delete( header, offset )
delete( index )

Used to remove a pair from the row by `header` or `index`. The pair is located as described in FasterCSV::Row.field(). The deleted pair is returned, or `nil` if a pair could not be found.

delete_if(&block)

The provided `block` is passed a header and field for each pair in the row and expected to return `true` or `false`, depending on whether the pair should be deleted.

This method returns the row for chaining.

each(&block)

Yields each pair of the row as header and field tuples (much like iterating over a Hash).

Support for Enumerable.

This method returns the row for chaining.

field( header )
field( header, offset )
field( index )

This method will fetch the field value by `header` or `index`. If a field is not found, `nil` is returned.

When provided, `offset` ensures that a header match occurrs on or later than the `offset` index. You can use this to find duplicate headers, without resorting to hard-coding exact indices.

field?(data)

Returns `true` if `data` matches a field in this row, and `false` otherwise.

field_row?()

Returns `true` if this is a field row.

fields(*headers_and_or_indices)

This method accepts any number of arguments which can be headers, indices, Ranges of either, or two-element Arrays containing a header and offset. Each argument will be replaced with a field lookup as described in FasterCSV::Row.field().

If called with no arguments, all fields are returned.

header?(name)

Returns `true` if `name` is a header for this row, and `false` otherwise.

header_row?()

Returns `true` if this is a header row.

headers()

Returns the headers of this row.

include?(name)

Alias for header?

index( header )
index( header, offset )

This method will return the index of a field with the provided `header`. The `offset` can be used to locate duplicate header names, as described in FasterCSV::Row.field().

inspect()

A summary of fields, by header.

push(*args)

A shortcut for appending multiple fields. Equivalent to:

      args.each { |arg| faster_csv_row << arg }

This method returns the row for chaining.

to_csv(options = Hash.new)

Returns the row as a CSV String. Headers are not used. Equivalent to:

      faster_csv_row.fields.to_csv( options )

to_hash()

Collapses the row into a simple Hash. Be warning that this discards field order and clobbers duplicate fields.

to_s(options = Hash.new)

Alias for to_csv

values_at(*headers_and_or_indices)

Alias for fields

[Validate]