Skip to content

Maintenance Ruby 3.3 gets fixes, but no new features. Switch to Ruby 4.0 →

instance method

CSV::Table#headers

table.headers → array_of_headers

Returns a new Array containing the String headers for the table.

If the table is not empty, returns the headers from the first row:

Example
rows = [
  CSV::Row.new(['Foo', 'Bar'], []),
  CSV::Row.new(['FOO', 'BAR'], []),
  CSV::Row.new(['foo', 'bar'], []),
]
table  = CSV::Table.new(rows)
table.headers # => ["Foo", "Bar"]
table.delete(0)
table.headers # => ["FOO", "BAR"]
table.delete(0)
table.headers # => ["foo", "bar"]

If the table is empty, returns a copy of the headers in the table itself:

Example
table.delete(0)
table.headers # => ["Foo", "Bar"]
See also #each #inspect #dig #push also in 3.2 · 3.1 · 3.0