From: yosfik@... Date: 2018-08-22T09:48:53+00:00 Subject: [ruby-core:88605] [Ruby trunk Bug#15001] Peek causes cursor to advance when enumerating the lines of a file Issue #15001 has been updated by ysfk (Yosfik Alqadri). with slight modification of the above code, ~~~ ruby e = File.open("f").each_line p e.each.to_a e.peek p e.each.to_a ~~~ it will give following result ~~~ text ["1\n", "2\n", "3\n"] Traceback (most recent call last): 1: from f.rb:3:in `
' f.rb:3:in `peek': iteration reached an end (StopIteration) ~~~ while the string version, ~~~ ruby e = "1\n2\n3\n".each_line p e.each.to_a e.peek p e.each.to_a ~~~ will give following result ~~~ text ["1\n", "2\n", "3\n"] ["1\n", "2\n", "3\n"] ~~~ ---------------------------------------- Bug #15001: Peek causes cursor to advance when enumerating the lines of a file https://bugs.ruby-lang.org/issues/15001#change-73661 * Author: fletchto99 (Matthew Langlois) * Status: Open * Priority: Normal * Assignee: * Target version: * ruby -v: ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin17] * Backport: 2.3: UNKNOWN, 2.4: UNKNOWN, 2.5: UNKNOWN ---------------------------------------- When a call to peek is made on the enumerator for the lines of a file the cursor advances. File: ~~~ text 1 2 3 ~~~ Sample script: ~~~ ruby 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. ~~~ ruby e = "1\n2\n3\n".each_line e.peek p e.each.to_a ~~~ The output is: => ["1\n", "2\n", "3\n"] -- https://bugs.ruby-lang.org/ Unsubscribe: