From: "mame (Yusuke Endoh)" Date: 2022-06-09T09:48:02+00:00 Subject: [ruby-core:108830] [Ruby master Bug#18767] IO.foreach hangs up when passes limit=0 Issue #18767 has been updated by mame (Yusuke Endoh). Status changed from Open to Rejected I believe this is not a bug. I don't think the current behavior is very useful, but I don't think it is so problematic either. Considering the following example, the behavior is consistent in a sense. ``` irb(main):001:0> File.write("foo.txt", [*?A..?Z].join) => 26 irb(main):002:0> File.foreach("foo.txt", 4).take(4) => ["ABCD", "EFGH", "IJKL", "MNOP"] irb(main):003:0> File.foreach("foo.txt", 3).take(4) => ["ABC", "DEF", "GHI", "JKL"] irb(main):004:0> File.foreach("foo.txt", 2).take(4) => ["AB", "CD", "EF", "GH"] irb(main):005:0> File.foreach("foo.txt", 1).take(4) => ["A", "B", "C", "D"] irb(main):006:0> File.foreach("foo.txt", 0).take(4) => ["", "", "", ""] ``` @andrykonchin If you really want to change this behavior, please open a feature request with an explanation of a real-world use case. ---------------------------------------- Bug #18767: IO.foreach hangs up when passes limit=0 https://bugs.ruby-lang.org/issues/18767#change-97906 * Author: andrykonchin (Andrew Konchin) * Status: Rejected * Priority: Normal * ruby -v: 3.0.3 * Backport: 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN ---------------------------------------- `IO.foreach` behaves in an unexpected way in a corner case when passes 0 as a limit parameter. It never stops and hangs up. ```ruby IO.foreach('file.txt', 0) { |s| p s } "" "" "" "" "" "" "" "" "" "" ``` Expected behavior - to raise ArgumentError "invalid limit: 0" like it does the `IO.readlines` method. I observe this behavior on 2.6, 2.7, and 3.0. Didn't check on 3.1 and master though. -- https://bugs.ruby-lang.org/ Unsubscribe: