Extended maintenance of Ruby versions 1.8.7 and 1.9.2 ended on July 31, 2014. Read more

In Files

  • csv.rb

Class/Module Index [+]



A CSV::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 CSV will be constructed from this class, if header row processing is activated.



Internal data format used to compare equality.

Public Class Methods

new(headers, fields, header_row = false) click to toggle source

Construct a new CSV::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 #header_row? and #field_row?, that this is a header row. Otherwise, the row is assumes to be a field row.

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

  • empty?()

  • length()

  • size()

               # File csv.rb, line 226
def initialize(headers, fields, header_row = false)
  @header_row = header_row

  # handle extra headers or fields
  @row = if headers.size > fields.size
    fields.zip(headers).map { |pair| pair.reverse }

Public Instance Methods

<<( field ) click to toggle source
<<( 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.

               # File csv.rb, line 330
def <<(arg)
  if arg.is_a?(Array) and arg.size == 2  # appending a header and name
    @row << arg
  elsif arg.is_a?(Hash)                  # append header and name pairs
    arg.each { |pair| @row << pair }
  else                                   # append field value
    @row << [nil, arg]

  self  # for chaining
==(other) click to toggle source

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

               # File csv.rb, line 467
def ==(other)
  @row == other.row
[](header_or_index, minimum_index = 0) click to toggle source
Alias for: field
[]=( header, value ) click to toggle source
[]=( header, offset, value )
[]=( index, value )

Looks up the field by the semantics described in #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.

               # File csv.rb, line 297
def []=(*args)
  value = args.pop

  if args.first.is_a? Integer
    if @row[args.first].nil?  # extending past the end with index
      @row[args.first] = [nil, value]
      @row.map! { |pair| pair.nil? ? [nil, nil] : pair }
    else                      # normal index assignment
      @row[args.first][1] = value
    index = index(*args)
    if index.nil?             # appending a field
      self << [args.first, value]
    else                      # normal header assignment
      @row[index][1] = value
delete( header ) click to toggle source
delete( header, offset )
delete( index )

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

               # File csv.rb, line 365
def delete(header_or_index, minimum_index = 0)
  if header_or_index.is_a? Integer                 # by index
  elsif i = index(header_or_index, minimum_index)  # by header
    [ ]
delete_if(&block) click to toggle source

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.

               # File csv.rb, line 382
def delete_if(&block)

  self  # for chaining
each(&block) click to toggle source

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.

               # File csv.rb, line 457
def each(&block)

  self  # for chaining
field( header ) click to toggle source
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.

               # File csv.rb, line 274
def field(header_or_index, minimum_index = 0)
  # locate the pair
  finder = header_or_index.is_a?(Integer) ? :[] : :assoc
  pair   = @row[minimum_index..-1].send(finder, header_or_index)

  # return the field if we have a pair
  pair.nil? ? nil : pair.last
Also aliased as: []
field?(data) click to toggle source

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

               # File csv.rb, line 443
def field?(data)
  fields.include? data
field_row?() click to toggle source

Returns true if this is a field row.

               # File csv.rb, line 252
def field_row?
  not header_row?
fields(*headers_and_or_indices) click to toggle source

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 #field.

If called with no arguments, all fields are returned.

               # File csv.rb, line 396
def fields(*headers_and_or_indices)
  if headers_and_or_indices.empty?  # return all fields--no arguments
    @row.map { |pair| pair.last }
  else                              # or work like values_at()
    headers_and_or_indices.inject(Array.new) do |all, h_or_i|
      all + if h_or_i.is_a? Range
        index_begin = h_or_i.begin.is_a?(Integer) ? h_or_i.begin :
        index_end   = h_or_i.end.is_a?(Integer)   ? h_or_i.end :
        new_range   = h_or_i.exclude_end? ? (index_begin...index_end) :
Also aliased as: values_at
header?(name) click to toggle source

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

               # File csv.rb, line 434
def header?(name)
  headers.include? name
Also aliased as: include?
header_row?() click to toggle source

Returns true if this is a header row.

               # File csv.rb, line 247
def header_row?
headers() click to toggle source

Returns the headers of this row.

               # File csv.rb, line 257
def headers
  @row.map { |pair| pair.first }
include?(name) click to toggle source
Alias for: header?
index( header ) click to toggle source
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 #field.

               # File csv.rb, line 426
def index(header, minimum_index = 0)
  # find the pair
  index = headers[minimum_index..-1].index(header)
  # return the index at the right offset, if we found one
  index.nil? ? nil : index + minimum_index
inspect() click to toggle source

A summary of fields, by header, in an ASCII compatible String.

               # File csv.rb, line 491
def inspect
  str = ["#<", self.class.to_s]
  each do |header, field|
    str << " " << (header.is_a?(Symbol) ? header.to_s : header.inspect) <<
           ":" << field.inspect
  str << ">"
  rescue  # any encoding error
    str.map do |s|
      e = Encoding::Converter.asciicompat_encoding(s.encoding)
      e ? s.encode(e) : s.force_encoding("ASCII-8BIT")
push(*args) click to toggle source

A shortcut for appending multiple fields. Equivalent to:

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

This method returns the row for chaining.

               # File csv.rb, line 349
def push(*args)
  args.each { |arg| self << arg }

  self  # for chaining
to_csv(options = Hash.new) click to toggle source

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

csv_row.fields.to_csv( options )
               # File csv.rb, line 485
def to_csv(options = Hash.new)
Also aliased as: to_s
to_hash() click to toggle source

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

               # File csv.rb, line 475
def to_hash
  # flatten just one level of the internal Array
  Hash[*@row.inject(Array.new) { |ary, pair| ary.push(*pair) }]
to_s(options = Hash.new) click to toggle source
Alias for: to_csv
values_at(*headers_and_or_indices) click to toggle source
Alias for: fields