Skip to content

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

instance method

CSV::Table#[]

table[n] → row or column_data
table[range] → array_of_rows or array_of_column_data
table[header] → array_of_column_data

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


Fetch a Row by Its Integer Index
  • Form: table[n], n an integer.

  • Access mode: :row or :col_or_row.

  • Return value: nth row of the table, if that row exists; otherwise nil.

Returns the nth row of the table if that row exists:

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:

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

Fetch a Column by Its Integer Index
  • Form: table[n], n an Integer.

  • Access mode: :col.

  • Return value: nth column of the table, if that column exists; otherwise an Array of nil fields of length self.size.

Returns the nth column of the table if that column exists:

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[1] # => ["0", "1", "2"]

Counts backward from the last column if n is negative:

Example
table[-2] # => ["foo", "bar", "baz"]

Returns an Array of nil fields if n is too large or too small:

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

Fetch Rows by Range
  • Form: table[range], range a Range object.

  • Access mode: :row or :col_or_row.

  • Return value: rows from the table, beginning at row range.start, if those rows exists.

Returns rows from the table, beginning at row range.first, if those rows exist:

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.start 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

Fetch Columns by Range
  • Form: table[range], range a Range object.

  • Access mode: :col.

  • Return value: column data from the table, beginning at column range.start, if those columns exist.

Returns column values from the table, if the column exists; the values are arranged by row:

Example
source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
table = CSV.parse(source, headers: true)
table.by_col!
table[0..1] # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]

Special case: if range.start == headers.size, returns an Array (size: table.size) of empty Arrays:

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

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

Example
table[0..-1] # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]

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

Example
table[-2..2] # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]

If range.start is larger than table.size, returns an Array of nil values:

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

Fetch a Column by Its String Header
  • Form: table[header], header a String header.

  • Access mode: :col or :col_or_row

  • Return value: column data from the table, if that header exists.

Returns column values from the table, if the column exists:

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.1 · 3.0