End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
CSV::Table#[]
# File tmp/rubies/ruby-3.0.5/lib/csv/table.rb, line 232
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.
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:
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-convertible object.
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:
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:
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
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:
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]