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
lib/csv/row.rb
View on GitHub ↗
# File tmp/rubies/ruby-3.3.0/lib/csv/row.rb, line 573
def index(header, minimum_index = 0)
# find the pair
index = headers[minimum_index..-1].index(header)
# return the index at the right offset, if we found one
index.nil? ? nil : index + minimum_index
end
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