Skip to content

CSV.each rewinding and including header row #68

Closed
@maschwenk

Description

@maschwenk

Reproduction script

require 'csv'

data = CSV.new(<<~ROWS, headers: true)
  Name,Department,Salary
  Bob,Engineering,1000
ROWS

data.each do |row|
  puts row.to_s
end

puts 'second loop'

data.each do |row|
  puts row.to_s
end

ruby 2.6.0p0 (2018-12-25 revision 66547) [x86_64-darwin18]

ruby csv_bug.rb

Bob,Engineering,1000
second loop
Name,Department,Salary  <--- 🔥this should not be here 🔥 
Bob,Engineering,1000

ruby 2.5.3p105 (2018-10-18 revision 65156) [x86_64-darwin18]

ruby csv_bug.rb

Bob,Engineering,1000
second loop

ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-darwin17]

ruby csv_bug.rb

Bob,Engineering,1000
second loop

What I think is going on

I may take a crack at fixing myself, but almost certain this isn't desired behavior.

It seems the row pointer is being rewound when calling .each twice, but on the second iteration the rewind is including the header line -- even though I've specified headers: true

Side note, in the future I think .each should always rewind the row counter after execution, which appears what it's trying to do in 2.6.0 but it's accidentally including the header row


Side note

Bob should ask for a raise

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions