Skip to content

Commit acc0511

Browse files
committed
All Enumerable based methods consume the same lines with other methods
This may have a performance penalty. We should benchmark this. GitHub: fix GH-260 Reported by Lhoussaine Ghallou. Thanks!!!
1 parent 71e6d24 commit acc0511

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

lib/csv.rb

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2551,7 +2551,13 @@ def header_convert(name = nil, &converter)
25512551
# p row
25522552
# end
25532553
def each(&block)
2554-
parser_enumerator.each(&block)
2554+
return to_enum(__method__) unless block_given?
2555+
begin
2556+
while true
2557+
yield(parser_enumerator.next)
2558+
end
2559+
rescue StopIteration
2560+
end
25552561
end
25562562

25572563
# :call-seq:
@@ -2586,14 +2592,7 @@ def each(&block)
25862592
# # Raises IOError (not opened for reading)
25872593
# csv.read
25882594
def read
2589-
rows = []
2590-
enumerator = parser_enumerator
2591-
begin
2592-
while true
2593-
rows << enumerator.next
2594-
end
2595-
rescue StopIteration
2596-
end
2595+
rows = to_a
25972596
if parser.use_headers?
25982597
Table.new(rows, headers: parser.headers)
25992598
else

0 commit comments

Comments
 (0)