Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Jee-El opened this issue Sep 5, 2022 · 1 comment

Comments

@Jee-El
Copy link

Jee-El commented Sep 5, 2022

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 :

  • Open a csv file with CSV.open('file.csv')
  • Call #pos on it, it returns 0
  • Call #eof? on it, it returns false
  • Call #each, #map, or #filter on it
  • Call #pos on it, it returns the last position
  • Call #eof? on it, it returns false when it should be true

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 #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 kou closed this as completed in acc0511 Sep 8, 2022
@kou
Copy link
Member

kou commented Sep 8, 2022

Thanks. I've fixed this.

peterzhu2118 pushed a commit to Shopify/ruby that referenced this issue Dec 6, 2022
…ther methods

This may have a performance penalty. We should benchmark this.

GitHub: fix ruby/csv#260

Reported by Lhoussaine Ghallou. Thanks!!!

ruby/csv@acc05116c5
nobu pushed a commit to nobu/ruby that referenced this issue May 24, 2023
…ther methods

This may have a performance penalty. We should benchmark this.

GitHub: fix ruby/csv#260

Reported by Lhoussaine Ghallou. Thanks!!!

ruby/csv@acc05116c5
nobu pushed a commit to ruby/ruby that referenced this issue May 24, 2023
…ther methods

This may have a performance penalty. We should benchmark this.

GitHub: fix ruby/csv#260

Reported by Lhoussaine Ghallou. Thanks!!!

ruby/csv@acc05116c5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants