Skip to content

End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

CSV::Table#[]

table[n] → row
table[range] → array_of_rows
table[header] → array_of_fields

Returns data from the table; does not modify the table.


The expression table[n], where n is a non-negative Integer, returns the nth row of the table, if that row exists, and if the access mode is :row or :col_or_row:

Example
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>
table[1] # => #<CSV::Row "Name":"bar" "Value":"1">
table.by_col_or_row! # => #<CSV::Table mode:col_or_row row_count:4>
table[1] # => #<CSV::Row "Name":"bar" "Value":"1">

Counts backward from the last row if n is negative:

Example
table[-1] # => #<CSV::Row "Name":"baz" "Value":"2">

Returns nil if n is too large or too small:

Example
table[4] # => nil
table[-4] => nil

Raises an exception if the access mode is :row and n is not an Integer-convertible object.

Example
table.by_row! # => #<CSV::Table mode:row row_count:4>
# Raises TypeError (no implicit conversion of String into Integer):
table['Name']

The expression table[range], where range is a Range object, returns rows from the table, beginning at row range.first, if those rows exist, and if the access mode is :row or :col_or_row:

Example
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>
rows = table[1..2] # => #<CSV::Row "Name":"bar" "Value":"1">
rows # => [#<CSV::Row "Name":"bar" "Value":"1">, #<CSV::Row "Name":"baz" "Value":"2">]
table.by_col_or_row! # => #<CSV::Table mode:col_or_row row_count:4>
rows = table[1..2] # => #<CSV::Row "Name":"bar" "Value":"1">
rows # => [#<CSV::Row "Name":"bar" "Value":"1">, #<CSV::Row "Name":"baz" "Value":"2">]

If there are too few rows, returns all from range.first to the end:

Example
rows = table[1..50] # => #<CSV::Row "Name":"bar" "Value":"1">
rows # => [#<CSV::Row "Name":"bar" "Value":"1">, #<CSV::Row "Name":"baz" "Value":"2">]

Special case: if range.start == table.size, returns an empty Array:

Example
table[table.size..50] # => []

If range.end is negative, calculates the ending index from the end:

Example
rows = table[0..-1]
rows # => [#<CSV::Row "Name":"foo" "Value":"0">, #<CSV::Row "Name":"bar" "Value":"1">, #<CSV::Row "Name":"baz" "Value":"2">]

If range.start is negative, calculates the starting index from the end:

Example
rows = table[-1..2]
rows # => [#<CSV::Row "Name":"baz" "Value":"2">]

If range.start is larger than table.size, returns nil:

Example
table[4..4] # => nil

The expression table[header], where header is a String, returns column values (Array of Strings) if the column exists and if the access mode is :col or :col_or_row:

Example
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>
table['Name'] # => ["foo", "bar", "baz"]
table.by_col_or_row! # => #<CSV::Table mode:col_or_row row_count:4>
col = table['Name']
col # => ["foo", "bar", "baz"]

Modifying the returned column values does not modify the table:

Example
col[0] = 'bat'
col # => ["bat", "bar", "baz"]
table['Name'] # => ["foo", "bar", "baz"]

Returns an Array of nil values if there is no such column:

Example
table['Nosuch'] # => [nil, nil, nil]
See also #[]= #== #<< #by_col also in 3.3 · 3.2 · 3.1