https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112020-07-17T16:26:40ZRuby Issue Tracking SystemRuby master - Bug #17034: Unexpected behavior in #max for beginless rangehttps://bugs.ruby-lang.org/issues/17034?journal_id=865842020-07-17T16:26:40Zcitizen428 (Michael Kohl)citizen428@gmail.com
<ul><li><strong>File</strong> <a href="/attachments/8497">range-max-beginless.patch</a> <a class="icon-only icon-download" title="Download" href="/attachments/download/8497/range-max-beginless.patch">range-max-beginless.patch</a> added</li></ul><p>citizen428 (Michael Kohl) wrote:</p>
<blockquote>
<p>I think this is not ideal. Possible solutions:</p>
<ol>
<li>Return <code>e</code> (<code>RANGE_END(range)</code> for beginless ranges or</li>
<li>return a <code>RangeError</code> with a message like "cannot get the maximum of beginless range" (similar to <code>.min</code>).</li>
</ol>
</blockquote>
<p>I had some time and looked into this. For inclusive integer ranges <code>r.end</code> will now be returned, no other behavior changes. Patch attached, but since the diff is very small I also opened a PR:</p>
<p><a href="https://github.com/ruby/ruby/pull/3328/" class="external">https://github.com/ruby/ruby/pull/3328/</a></p> Ruby master - Bug #17034: Unexpected behavior in #max for beginless rangehttps://bugs.ruby-lang.org/issues/17034?journal_id=865852020-07-17T17:48:41Zjeremyevans0 (Jeremy Evans)merch-redmine@jeremyevans.net
<ul></ul><p>citizen428 (Michael Kohl) wrote:</p>
<blockquote>
<p>I think this is not ideal. Possible solutions:</p>
<ol>
<li>Return <code>e</code> (<code>RANGE_END(range)</code> for beginless ranges or</li>
<li>return a <code>RangeError</code> with a message like "cannot get the maximum of beginless range" (similar to <code>.min</code>).</li>
</ol>
<p>Happy to provide a patch if people want this changed and can agree on what the new behavior should be.</p>
</blockquote>
<p>I think a <code>RangeError</code> is more correct. Without having a beginning, you cannot know the increment value, and therefore cannot know the maximum value. People that want the end of the range should use <code>Range#end</code>, not <code>Range#max</code>. However, I can see where <code>Range#max</code> returning the end for an inclusive beginless range could potentially be more useful.</p> Ruby master - Bug #17034: Unexpected behavior in #max for beginless rangehttps://bugs.ruby-lang.org/issues/17034?journal_id=865902020-07-18T00:59:25Zcitizen428 (Michael Kohl)citizen428@gmail.com
<ul></ul><p>jeremyevans0 (Jeremy Evans) wrote in <a href="#note-2">#note-2</a>:</p>
<blockquote>
<p>I think a <code>RangeError</code> is more correct. Without having a beginning, you cannot know the increment value, and therefore cannot know the maximum value. People that want the end of the range should use <code>Range#end</code>, not <code>Range#max</code>. However, I can see where <code>Range#max</code> returning the end for an inclusive beginless range could potentially be more useful.</p>
</blockquote>
<p>The current PR only returns the last value if it's an inclusive beginless range of integers:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="p">(</span><span class="o">..</span><span class="mi">2</span><span class="p">).</span><span class="nf">max</span>
<span class="c1">#=> 2</span>
</code></pre>
<p>Exclusive range (<code>(...2).max</code>): TypeError (cannot exclude end value with non Integer begin value)<br>
Non-integer range (<code>(...2.0).max</code>): TypeError (cannot exclude non Integer end value)<br>
Non-numeric range (<code>(...'c').max</code>): TypeError (cannot get the maximum of beginless range with custom comparison method)</p>
<p>That said I'm happy to change it to a <code>RangeError</code> if that's the overall consensus.</p> Ruby master - Bug #17034: Unexpected behavior in #max for beginless rangehttps://bugs.ruby-lang.org/issues/17034?journal_id=865942020-07-18T16:12:28Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul><li><strong>Backport</strong> changed from <i>2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN</i> to <i>2.5: DONTNEED, 2.6: DONTNEED, 2.7: REQUIRED</i></li></ul><p>I agree that <code>RangeError</code> would be better, but it seems a separate issue as <code>TypeError</code> is used already in other cases.</p> Ruby master - Bug #17034: Unexpected behavior in #max for beginless rangehttps://bugs.ruby-lang.org/issues/17034?journal_id=865952020-07-18T16:19:00ZAnonymous
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Closed</i></li></ul><p>Applied in changeset <a class="changeset" title="Fix Range#max for beginless Integer ranges [Bug #17034] * Fix Range#max for beginless Integer ra..." href="https://bugs.ruby-lang.org/projects/ruby-master/repository/git/revisions/8a5ad2b77d7a24e4f8f4fef179ae5efced935f91">git|8a5ad2b77d7a24e4f8f4fef179ae5efced935f91</a>.</p>
<hr>
<p>Fix Range#max for beginless Integer ranges [Bug <a class="issue tracker-1 status-5 priority-4 priority-default closed" title="Bug: Unexpected behavior in #max for beginless range (Closed)" href="https://bugs.ruby-lang.org/issues/17034">#17034</a>]</p>
<ul>
<li>Fix Range#max for beginless Integer ranges</li>
<li>Update test/ruby/test_range.rb</li>
<li>Fix formatting</li>
</ul>
<p><a href="https://github.com/ruby/ruby/pull/3328" class="external">https://github.com/ruby/ruby/pull/3328</a></p>
<p>Co-authored-by: Nobuyoshi Nakada <a href="mailto:nobu@ruby-lang.org" class="email">nobu@ruby-lang.org</a></p>