https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112022-05-10T12:12:54ZRuby Issue Tracking SystemRuby master - Bug #18768: Inconsistent behavior of IO, StringIO and String each_line methods when return paragraph and chomp: true passedhttps://bugs.ruby-lang.org/issues/18768?journal_id=975452022-05-10T12:12:54Zandrykonchin (Andrew Konchin)
<ul><li><strong>Description</strong> updated (<a title="View differences" href="/journals/97545/diff?detail_id=62487">diff</a>)</li></ul> Ruby master - Bug #18768: Inconsistent behavior of IO, StringIO and String each_line methods when return paragraph and chomp: true passedhttps://bugs.ruby-lang.org/issues/18768?journal_id=977622022-05-26T18:42:15Zjeremyevans0 (Jeremy Evans)merch-redmine@jeremyevans.net
<ul><li><strong>Related to</strong> <i><a class="issue tracker-1 status-5 priority-4 priority-default closed" href="/issues/18770">Bug #18770</a>: Inconsistent behavior of IO/StringIO's each methods when called with nil as a separator, limit and chomp: true</i> added</li></ul> Ruby master - Bug #18768: Inconsistent behavior of IO, StringIO and String each_line methods when return paragraph and chomp: true passedhttps://bugs.ruby-lang.org/issues/18768?journal_id=977692022-05-26T22:16:26Zjeremyevans0 (Jeremy Evans)merch-redmine@jeremyevans.net
<ul></ul><p>I agree that the behavior for all of these methods should be the same. I think the IO behavior makes the most sense, and String and StringIO should be changed to match it.</p>
<p>I submitted pull requests to Ruby (<a href="https://github.com/ruby/ruby/pull/5960" class="external">https://github.com/ruby/ruby/pull/5960</a>) and StringIO (<a href="https://github.com/ruby/stringio/pull/29" class="external">https://github.com/ruby/stringio/pull/29</a>) to fix this.</p> Ruby master - Bug #18768: Inconsistent behavior of IO, StringIO and String each_line methods when return paragraph and chomp: true passedhttps://bugs.ruby-lang.org/issues/18768?journal_id=977972022-05-30T16:00:30Zjeremyevans (Jeremy Evans)code@jeremyevans.net
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Closed</i></li></ul><p>Applied in changeset <a class="changeset" title="[ruby/stringio] Fix handling of chomp with paragraph separator Try to mirror IO behavior, where ..." href="https://bugs.ruby-lang.org/projects/ruby-master/repository/git/revisions/609d73e8925f807786686caf635178bb1de74256">git|609d73e8925f807786686caf635178bb1de74256</a>.</p>
<hr>
<p>[ruby/stringio] Fix handling of chomp with paragraph separator</p>
<p>Try to mirror IO behavior, where chomp takes out the entire paragraph<br>
separators between entries, but does not chomp a single line separator<br>
at the end of the string.</p>
<p>Partially Fixes [Bug <a class="issue tracker-1 status-5 priority-4 priority-default closed" title="Bug: Inconsistent behavior of IO, StringIO and String each_line methods when return paragraph and chom... (Closed)" href="https://bugs.ruby-lang.org/issues/18768">#18768</a>]</p>
<p><a href="https://github.com/ruby/stringio/commit/a83ddbb7f0" class="external">https://github.com/ruby/stringio/commit/a83ddbb7f0</a></p> Ruby master - Bug #18768: Inconsistent behavior of IO, StringIO and String each_line methods when return paragraph and chomp: true passedhttps://bugs.ruby-lang.org/issues/18768?journal_id=978512022-06-06T17:38:23Zjeremyevans0 (Jeremy Evans)merch-redmine@jeremyevans.net
<ul><li><strong>Status</strong> changed from <i>Closed</i> to <i>Open</i></li></ul> Ruby master - Bug #18768: Inconsistent behavior of IO, StringIO and String each_line methods when return paragraph and chomp: true passedhttps://bugs.ruby-lang.org/issues/18768?journal_id=979082022-06-09T10:16:01Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul></ul><p>I have no strong opinion about this issue and am never against Jeremy's change, but I'm just curious. I thought that <code>each_line(..., chomp: true) {|s| ... }</code> was equal to <code>each_line(...) {|s| s = s.chomp; ... }</code>. In the above examples, StringIO's result (<code>["a\n", "b\n", "c"]</code>) was the simplest to me. <a class="user active user-mention" href="https://bugs.ruby-lang.org/users/1604">@jeremyevans0 (Jeremy Evans)</a> How did you think the IO behavior made the most sense?</p> Ruby master - Bug #18768: Inconsistent behavior of IO, StringIO and String each_line methods when return paragraph and chomp: true passedhttps://bugs.ruby-lang.org/issues/18768?journal_id=979132022-06-09T15:20:39Zjeremyevans0 (Jeremy Evans)merch-redmine@jeremyevans.net
<ul></ul><p>mame (Yusuke Endoh) wrote in <a href="#note-6">#note-6</a>:</p>
<blockquote>
<p>I have no strong opinion about this issue and am never against Jeremy's change, but I'm just curious. I thought that <code>each_line(..., chomp: true) {|s| ... }</code> was equal to <code>each_line(...) {|s| s = s.chomp; ... }</code>. In the above examples, StringIO's result (<code>["a\n", "b\n", "c"]</code>) was the simplest to me. <a class="user active user-mention" href="https://bugs.ruby-lang.org/users/1604">@jeremyevans0 (Jeremy Evans)</a> How did you think the IO behavior made the most sense?</p>
</blockquote>
<p>My thought here is that <code>chomp: true</code>, does not mean <code>chomp</code> with no arguments, it means <code>chomp</code> with the separator used for the <code>each_line</code>. So when you call each_line with <code>''</code> (paragraph separator), it only chomps the paragraph separator. That's what the IO behavior already was, and I thought it best to make String/StringIO consistent with that.</p> Ruby master - Bug #18768: Inconsistent behavior of IO, StringIO and String each_line methods when return paragraph and chomp: true passedhttps://bugs.ruby-lang.org/issues/18768?journal_id=979262022-06-10T09:39:54Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul></ul><p><a class="user active user-mention" href="https://bugs.ruby-lang.org/users/1604">@jeremyevans0 (Jeremy Evans)</a> Thank you for your explanation, I understand. The keyword name "chomp" might be a bit confusing.</p> Ruby master - Bug #18768: Inconsistent behavior of IO, StringIO and String each_line methods when return paragraph and chomp: true passedhttps://bugs.ruby-lang.org/issues/18768?journal_id=979322022-06-10T15:46:51Zjeremyevans0 (Jeremy Evans)merch-redmine@jeremyevans.net
<ul></ul><p>mame (Yusuke Endoh) wrote in <a href="#note-8">#note-8</a>:</p>
<blockquote>
<p><a class="user active user-mention" href="https://bugs.ruby-lang.org/users/1604">@jeremyevans0 (Jeremy Evans)</a> Thank you for your explanation, I understand. The keyword name "chomp" might be a bit confusing.</p>
</blockquote>
<p>I agree, the behavior is not obvious from the name <code>chomp</code>. However, it is obvious that <code>chomp</code> is supposed to remove the separator and not the generic newline. You can see this when providing an explicit line ending character:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="s1">'abc'</span><span class="p">.</span><span class="nf">each_line</span><span class="p">(</span><span class="s1">'b'</span><span class="p">,</span> <span class="ss">chomp: </span><span class="kp">true</span><span class="p">).</span><span class="nf">to_a</span>
<span class="c1"># => ["a", "c"]</span>
<span class="nb">require</span> <span class="s1">'stringio'</span>
<span class="no">StringIO</span><span class="p">.</span><span class="nf">new</span><span class="p">(</span><span class="s1">'abc'</span><span class="p">).</span><span class="nf">each_line</span><span class="p">(</span><span class="s1">'b'</span><span class="p">,</span> <span class="ss">chomp: </span><span class="kp">true</span><span class="p">).</span><span class="nf">to_a</span>
<span class="c1"># => ["a", "c"]</span>
<span class="n">r</span><span class="p">,</span> <span class="n">w</span> <span class="o">=</span> <span class="no">IO</span><span class="p">.</span><span class="nf">pipe</span>
<span class="n">w</span><span class="p">.</span><span class="nf">write</span><span class="p">(</span><span class="s1">'abc'</span><span class="p">)</span>
<span class="n">w</span><span class="p">.</span><span class="nf">close</span>
<span class="n">r</span><span class="p">.</span><span class="nf">each_line</span><span class="p">(</span><span class="s1">'b'</span><span class="p">,</span> <span class="ss">chomp: </span><span class="kp">true</span><span class="p">).</span><span class="nf">to_a</span>
<span class="c1"># => ["a", "c"]</span>
</code></pre> Ruby master - Bug #18768: Inconsistent behavior of IO, StringIO and String each_line methods when return paragraph and chomp: true passedhttps://bugs.ruby-lang.org/issues/18768?journal_id=984132022-07-21T12:25:22Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul></ul><p>We discussed this at the dev meeting, and <a class="user active user-mention" href="https://bugs.ruby-lang.org/users/13">@matz (Yukihiro Matsumoto)</a> agreed with <a class="user active user-mention" href="https://bugs.ruby-lang.org/users/1604">@jeremyevans0 (Jeremy Evans)</a>: <code>chomp: true</code> should remove the separator. Could you please merge the change?</p> Ruby master - Bug #18768: Inconsistent behavior of IO, StringIO and String each_line methods when return paragraph and chomp: true passedhttps://bugs.ruby-lang.org/issues/18768?journal_id=984212022-07-21T15:02:54Zjeremyevans (Jeremy Evans)code@jeremyevans.net
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Closed</i></li></ul><p>Applied in changeset <a class="changeset" title="Make String#each_line work correctly with paragraph separator and chomp Previously, it was inclu..." href="https://bugs.ruby-lang.org/projects/ruby-master/repository/git/revisions/423b41cba77719b4f62aa530593ad36a990f7c74">git|423b41cba77719b4f62aa530593ad36a990f7c74</a>.</p>
<hr>
<p>Make String#each_line work correctly with paragraph separator and chomp</p>
<p>Previously, it was including one newline when chomp was used,<br>
which is inconsistent with IO#each_line behavior. This makes<br>
behavior consistent with IO#each_line, chomping all paragraph<br>
separators (multiple consecutive newlines), but not single<br>
newlines.</p>
<p>Partially Fixes [Bug <a class="issue tracker-1 status-5 priority-4 priority-default closed" title="Bug: Inconsistent behavior of IO, StringIO and String each_line methods when return paragraph and chom... (Closed)" href="https://bugs.ruby-lang.org/issues/18768">#18768</a>]</p>