ruby / csv

CSV Reading and Writing
https://ruby.github.io/csv/
BSD 2-Clause "Simplified" License
177 stars 113 forks source link

#eof? method returning wrong value when it's used on a csv file with #each, #map, #filter #260

Closed Jee-El closed 1 year ago

Jee-El commented 1 year ago

For reference : I cloned this repository on my local machine, then in my Gemfile I put :

# frozen_string_literal: true

source "https://rubygems.org"

gem 'csv', :path => "../csv"

The path is correct, the proof is that the former issue with #read is not happening anymore. I'm running the code with : bundle exec ruby main.rb

Ruby version : 3.1.2p20 Steps to reproduce the issue :

main.rb

require 'csv'

# it returns true on the second call of #eof? as expected only if
# I comment the 4th line, starting from the line below this
puts 'csv'
csv = CSV.open('event_attendees.csv')
puts "pos : #{csv.pos}" # expecting 0, getting 0
puts "eof? : #{csv.eof?}" # expecting false, getting false
csv.each { |row| row }
puts "pos : #{csv.pos}" # expecting 1814, getting 1814
puts "eof? : #{csv.eof?}" # expecting true, getting false
puts

# no issue when using File.open
puts 'File'
file = File.open('event_attendees.csv')
puts "pos : #{file.pos}" # expecting 0, getting 0
puts "eof? : #{file.eof?}" # expecting false, getting false
file.each { |row| row }
puts "pos : #{file.pos}" # expecting 1814, getting 1814
puts "eof? : #{file.eof?}" # expecting true, getting true

This is almost the same bug I mentioned in this issue https://github.com/ruby/csv/issues/258 (which got fixed), but instead of reading the file with #read, I'm using the #each method on the csv file, #map and #filter also cause the issue.

event_attendees.csv

kou commented 1 year ago

Thanks. I've fixed this.