From: "jeremyevans0 (Jeremy Evans)" Date: 2022-06-10T15:46:51+00:00 Subject: [ruby-core:108853] [Ruby master Bug#18768] Inconsistent behavior of IO, StringIO and String each_line methods when return paragraph and chomp: true passed Issue #18768 has been updated by jeremyevans0 (Jeremy Evans). mame (Yusuke Endoh) wrote in #note-8: > @jeremyevans0 Thank you for your explanation, I understand. The keyword name "chomp" might be a bit confusing. I agree, the behavior is not obvious from the name `chomp`. However, it is obvious that `chomp` is supposed to remove the separator and not the generic newline. You can see this when providing an explicit line ending character: ```ruby 'abc'.each_line('b', chomp: true).to_a # => ["a", "c"] require 'stringio' StringIO.new('abc').each_line('b', chomp: true).to_a # => ["a", "c"] r, w = IO.pipe w.write('abc') w.close r.each_line('b', chomp: true).to_a # => ["a", "c"] ``` ---------------------------------------- Bug #18768: Inconsistent behavior of IO, StringIO and String each_line methods when return paragraph and chomp: true passed https://bugs.ruby-lang.org/issues/18768#change-97932 * Author: andrykonchin (Andrew Konchin) * Status: Open * Priority: Normal * ruby -v: 3.0.3 * Backport: 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN ---------------------------------------- In IO, StringIO and String methods that return lines (e.g. `each`, `each_line`, `gets`, `readline`, `readlines`) behave in a different way when the following parameters are passed - separator is `""` and - `chomp` is `true`. They truncate the new line characters between paragraphs and the trailing one differently: ```ruby "a\n\nb\n\nc\n".each_line("", chomp: true).to_a #=> ["a\n", "b\n", "c\n"] StringIO.new("a\n\nb\n\nc\n").each_line("", chomp: true).to_a #=> ["a\n", "b\n", "c"] File.open('chomp.txt').each_line("", chomp: true).to_a #=> ["a", "b", "c\n"] ``` The text file content is the same: ```ruby File.read('chomp.txt') #=> "a\n\nb\n\nc\n" ``` Expected behavior - they return the same result. -- https://bugs.ruby-lang.org/ Unsubscribe: