From: "phasis68 (Heesob Park)" Date: 2013-11-27T16:05:07+09:00 Subject: [ruby-core:58630] [ruby-trunk - Bug #9164] IO::foreach incorrect work for binary file on windows platform Issue #9164 has been updated by phasis68 (Heesob Park). On Windows, "\x1A"(Control+Z) means end of file. You must use binary read mode. p File.foreach('1.txt',mode:'rb').to_a => ["123\n", "\x1A\xE8\xBA\b\n", "789\n"] ---------------------------------------- Bug #9164: IO::foreach incorrect work for binary file on windows platform https://bugs.ruby-lang.org/issues/9164#change-43199 Author: windwiny (wind winy) Status: Open Priority: Normal Assignee: Category: Target version: ruby -v: 2.0.0p353,2.1.0dev Backport: 1.9.3: UNKNOWN, 2.0.0: UNKNOWN d2 = "123\n".b + "\x1a\xe8\xba\b".b + "\n789\n".b File.binwrite('1.txt', d2) p File.binread('1.txt').each_line.to_a # => ["123\n", "\x1A\xE8\xBA\b\n", "789\n"] p File.foreach('1.txt').to_a # => ["123\n"] Encoding.default_external = Encoding::UTF_8 p File.foreach('1.txt').to_a # => ["123\n"] Encoding.default_external = Encoding::ASCII_8BIT p File.foreach('1.txt').to_a # => ["123\n"] run above code on windows, File.foreach.to_a just return 1 row. run on mac and linux, that's correct, return 3 rows. -- http://bugs.ruby-lang.org/