Skip to content

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

instance method

CSV::Row#index

index(header) → index
index(header, offset) → index

Returns the index for the given header, if it exists; otherwise returns nil.

With the single argument header, returns the index of the first-found field with the given header:

Example
source = "Name,Name,Name\nFoo,Bar,Baz\n"
table = CSV.parse(source, headers: true)
row = table[0]
row.index('Name') # => 0
row.index('NAME') # => nil

With arguments header and offset, returns the index of the first-found field with given header, but ignoring the first offset fields:

Example
row.index('Name', 1) # => 1
row.index('Name', 3) # => nil