ruby / csv

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

#eof? method returning wrong value when it's used on a csv file #258

Closed Jee-El closed 2 years ago

Jee-El commented 2 years ago

Ruby version : 3.1.2p20, as well as 2.7.4p191 (replit) Steps to reproduce the issue(?) :

It seems to return true if #eof? isn't called before reading the file. (i.e: omit the third line above)

Demonstration : https://replit.com/@Jee-El/Ruby-CSV-eof-issue?v=1

kou commented 2 years ago

Could you paste main.rb and attach event_attendees.csv here for easy to refer?

Jee-El commented 2 years ago

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
puts "eof? : #{csv.eof?}" # expecting false
csv.read
puts "pos : #{csv.pos}" # expecting 1814
puts "eof? : #{csv.eof?}" # expecting true
puts

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

event_attendees.csv

kou commented 2 years ago

Thanks. I've fixed this.