Skip to content

Commit 71e6d24

Browse files
committed
CSV#read consumes the same lines with other methods like CSV#shift
GitHub: fix GH-258 Reported by Lhoussaine Ghallou. Thanks!!!
1 parent a171621 commit 71e6d24

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

lib/csv.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2586,7 +2586,14 @@ def each(&block)
25862586
# # Raises IOError (not opened for reading)
25872587
# csv.read
25882588
def read
2589-
rows = to_a
2589+
rows = []
2590+
enumerator = parser_enumerator
2591+
begin
2592+
while true
2593+
rows << enumerator.next
2594+
end
2595+
rescue StopIteration
2596+
end
25902597
if parser.use_headers?
25912598
Table.new(rows, headers: parser.headers)
25922599
else

test/csv/parse/test_read.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# -*- coding: utf-8 -*-
2+
# frozen_string_literal: false
3+
4+
require_relative "../helper"
5+
6+
class TestCSVParseRead < Test::Unit::TestCase
7+
extend DifferentOFS
8+
9+
def test_shift
10+
data = <<-CSV
11+
1
12+
2
13+
3
14+
CSV
15+
csv = CSV.new(data)
16+
assert_equal([
17+
["1"],
18+
[["2"], ["3"]],
19+
nil,
20+
],
21+
[
22+
csv.shift,
23+
csv.read,
24+
csv.shift,
25+
])
26+
end
27+
end

0 commit comments

Comments
 (0)