class CSV::Table

CSV::Table

A CSV::Table instance represents CSV data. (see class CSV).

The instance may have: * Rows: each is a Table::Row object. * Headers: names for the columns.

Instance Methods

CSV::Table has three groups of instance methods: * Its own internally defined instance methods. * Methods included by module Enumerable. * Methods delegated to class Array.: * Array#empty? * Array#length * Array#size

Creating a CSV::Table Instance

Commonly, a new CSV::Table instance is created by parsing CSV source using headers: source = “Name,Value\nfoo,0\nbar,1\nbaz,2\n” table = CSV.parse(source, headers: true) table.class # => CSV::Table

You can also create an instance directly. See ::new.

Headers

If a table has headers, the headers serve as labels for the columns of data. Each header serves as the label for its column.

The headers for a CSV::Table object are stored as an Array of Strings.

Commonly, headers are defined in the first row of CSV source: source = “Name,Value\nfoo,0\nbar,1\nbaz,2\n” table = CSV.parse(source, headers: true) table.headers # => [“Name”, “Value”]

If no headers are defined, the Array is empty: table = CSV::Table.new([]) table.headers # => []

Access Modes

CSV::Table provides three modes for accessing table data: * Row mode. * Column mode. * Mixed mode (the default for a new table).

The access mode for aCSV::Table instance affects the behavior of some of its instance methods: * [] * []= * delete * delete_if * each * values_at

Row Mode

Set a table to row mode with method by_row!: source = “Name,Value\nfoo,0\nbar,1\nbaz,2\n” table = CSV.parse(source, headers: true) table.by_row! # => #<CSV::Table mode:row row_count:4>

Specify a single row by an Integer index: # Get a row. table # => #<CSV::Row “Name”:“bar” “Value”:“1”> # Set a row, then get it. table = CSV::Row.new([‘Name’, ‘Value’], [‘bam’, 3]) table # => #<CSV::Row “Name”:“bam” “Value”:3>

Specify a sequence of rows by a Range: # Get rows. table # => [#<CSV::Row “Name”:“bam” “Value”:3>, #<CSV::Row “Name”:“baz” “Value”:“2”>] # Set rows, then get them. table = [ CSV::Row.new([‘Name’, ‘Value’], [‘bat’, 4]), CSV::Row.new([‘Name’, ‘Value’], [‘bad’, 5]), ] table # => [[“Name”, #<CSV::Row “Name”:“bat” “Value”:4>], [“Value”, #<CSV::Row “Name”:“bad” “Value”:5>]]

Column Mode

Set a table to column mode with method by_col!: source = “Name,Value\nfoo,0\nbar,1\nbaz,2\n” table = CSV.parse(source, headers: true) table.by_col! # => #<CSV::Table mode:col row_count:4>

Specify a column by an Integer index: # Get a column. table # Set a column, then get it. table = [‘FOO’, ‘BAR’, ‘BAZ’] table # => [“FOO”, “BAR”, “BAZ”]

Specify a column by its String header: # Get a column. table # => [“FOO”, “BAR”, “BAZ”] # Set a column, then get it. table = [‘Foo’, ‘Bar’, ‘Baz’] table # => [“Foo”, “Bar”, “Baz”]

Mixed Mode

In mixed mode, you can refer to either rows or columns: * An Integer index refers to a row. * A Range index refers to multiple rows. * A String index refers to a column.

Set a table to mixed mode with method by_col_or_row!: source = “Name,Value\nfoo,0\nbar,1\nbaz,2\n” table = CSV.parse(source, headers: true) table.by_col_or_row! # => #<CSV::Table mode:col_or_row row_count:4>

Specify a single row by an Integer index: # Get a row. table # => #<CSV::Row “Name”:“bar” “Value”:“1”> # Set a row, then get it. table = CSV::Row.new([‘Name’, ‘Value’], [‘bam’, 3]) table # => #<CSV::Row “Name”:“bam” “Value”:3>

Specify a sequence of rows by a Range: # Get rows. table # => [#<CSV::Row “Name”:“bam” “Value”:3>, #<CSV::Row “Name”:“baz” “Value”:“2”>] # Set rows, then get them. table = CSV::Row.new([‘Name’, ‘Value’], [‘bat’, 4]) table = CSV::Row.new([‘Name’, ‘Value’], [‘bad’, 5]) table # => [[“Name”, #<CSV::Row “Name”:“bat” “Value”:4>], [“Value”, #<CSV::Row “Name”:“bad” “Value”:5>]]

Specify a column by its String header: # Get a column. table # => [“foo”, “bat”, “bad”] # Set a column, then get it. table = [‘Foo’, ‘Bar’, ‘Baz’] table # => [“Foo”, “Bar”, “Baz”]