Skip to content

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

instance method

CSV#lineno

csv.line_no → integer

Returns the count of the rows parsed or generated.

Parsing:

Example
string = "foo,0\nbar,1\nbaz,2\n"
path = 't.csv'
File.write(path, string)
CSV.open(path) do |csv|
  csv.each do |row|
    p [csv.lineno, row]
  end
end

Output:

Example
[1, ["foo", "0"]]
[2, ["bar", "1"]]
[3, ["baz", "2"]]

Generating:

Example
CSV.generate do |csv|
  p csv.lineno; csv << ['foo', 0]
  p csv.lineno; csv << ['bar', 1]
  p csv.lineno; csv << ['baz', 2]
end

Output:

Example
0
1
2