End of Life Ruby 3.1 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
CSV::Table#[]
# File tmp/rubies/ruby-3.1.3/lib/csv/table.rb, line 514
def [](index_or_header)
if @mode == :row or # by index
(@mode == :col_or_row and (index_or_header.is_a?(Integer) or index_or_header.is_a?(Range)))
@table[index_or_header]
else # by header
@table.map { |row| row[index_or_header] }
end
end
Returns data from the table; does not modify the table.
- Fetch a Row by Its Integer Index
-
Form:
table[n],nan integer. -
Access mode:
:rowor: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:
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:
table[-1] # => #<CSV::Row "Name":"baz" "Value":"2">
Returns nil if n is too large or too small:
table[4] # => nil table[-4] # => nil
Raises an exception if the access mode is :row and n is not an Integer:
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],nan Integer. -
Access mode:
:col. -
Return value: nth column of the table, if that column exists; otherwise an Array of
nilfields of lengthself.size.
Returns the nth column of the table if that column exists:
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:
table[-2] # => ["foo", "bar", "baz"]
Returns an Array of nil fields if n is too large or too small:
table[4] # => [nil, nil, nil] table[-4] # => [nil, nil, nil]
- Fetch Rows by Range
-
Form:
table[range],rangea Range object. -
Access mode:
:rowor: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:
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:
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:
table[table.size..50] # => []
If range.end is negative, calculates the ending index from the end:
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:
rows = table[-1..2] rows # => [#<CSV::Row "Name":"baz" "Value":"2">]
If range.start is larger than table.size, returns nil:
table[4..4] # => nil
- Fetch Columns by Range
-
Form:
table[range],rangea 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:
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:
table[table.headers.size..50] # => [[], [], []]
If range.end is negative, calculates the ending index from the end:
table[0..-1] # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
If range.start is negative, calculates the starting index from the end:
table[-2..2] # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
If range.start is larger than table.size, returns an Array of nil values:
table[4..4] # => [nil, nil, nil]
- Fetch a Column by Its String Header
-
Form:
table[header],headera String header. -
Access mode:
:color:col_or_row -
Return value: column data from the table, if that
headerexists.
Returns column values from the table, if the column exists:
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:
col[0] = 'bat' col # => ["bat", "bar", "baz"] table['Name'] # => ["foo", "bar", "baz"]
Returns an Array of nil values if there is no such column:
table['Nosuch'] # => [nil, nil, nil]