Project

General

Profile

Actions

Bug #15001

closed

Peek causes cursor to advance when enumerating the lines of a file

Added by fletchto99 (Matthew Langlois) over 5 years ago. Updated over 4 years ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin17]
[ruby-core:88507]

Description

When a call to peek is made on the enumerator for the lines of a file the cursor advances.

File:

1
2
3

Sample script:

e = File.open("f").each_line
e.peek
p e.each.to_a

The output is => ["2\n", "3\n"]
The expected output is => ["1\n", "2\n", "3\n"]

When performing the same operation on a string we get the expected output.

e = "1\n2\n3\n".each_line
e.peek
p e.each.to_a

The output is: => ["1\n", "2\n", "3\n"]

Actions #1

Updated by fletchto99 (Matthew Langlois) over 5 years ago

  • Subject changed from Peek causes cursor to advance when reading files to Peek causes cursor to advance when enumerating the lines of a file

Updated by ysfk (Yosfik Alqadri) over 5 years ago

with slight modification of the above code,

e = File.open("f").each_line
p e.each.to_a
e.peek
p e.each.to_a

it will give following result

["1\n", "2\n", "3\n"]
Traceback (most recent call last):
	1: from f.rb:3:in `<main>'
f.rb:3:in `peek': iteration reached an end (StopIteration)

while the string version,

e = "1\n2\n3\n".each_line
p e.each.to_a
e.peek
p e.each.to_a

will give following result

["1\n", "2\n", "3\n"]
["1\n", "2\n", "3\n"]

Updated by Hanmac (Hans Mackowiak) over 5 years ago

i think the problem is that String Enumerations are rewindable while on the IO/File it is not

Actions #4

Updated by jeremyevans0 (Jeremy Evans) over 4 years ago

  • Status changed from Open to Closed
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0