https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17097754782020-07-07T20:41:34ZRuby Issue Tracking SystemRuby master - Bug #17017: Range#max & Range#minmax incorrectly use Float end as maxhttps://bugs.ruby-lang.org/issues/17017?journal_id=864512020-07-07T20:41:34Zsambostock (Sam Bostock)
<ul><li><strong>Description</strong> updated (<a title="View differences" href="/journals/86451/diff?detail_id=57452">diff</a>)</li></ul><p>Add .to_a examples to clarify unexpected behaviour.</p> Ruby master - Bug #17017: Range#max & Range#minmax incorrectly use Float end as maxhttps://bugs.ruby-lang.org/issues/17017?journal_id=864522020-07-07T20:43:29Zsambostock (Sam Bostock)
<ul><li><strong>Description</strong> updated (<a title="View differences" href="/journals/86452/diff?detail_id=57453">diff</a>)</li></ul><p>Add full array example</p> Ruby master - Bug #17017: Range#max & Range#minmax incorrectly use Float end as maxhttps://bugs.ruby-lang.org/issues/17017?journal_id=864532020-07-07T20:56:41Zsambostock (Sam Bostock)
<ul><li><strong>Description</strong> updated (<a title="View differences" href="/journals/86453/diff?detail_id=57454">diff</a>)</li></ul><p>Add documentation change as an option</p> Ruby master - Bug #17017: Range#max & Range#minmax incorrectly use Float end as maxhttps://bugs.ruby-lang.org/issues/17017?journal_id=864882020-07-10T16:29:36Zjeremyevans0 (Jeremy Evans)merch-redmine@jeremyevans.net
<ul></ul><p>I've added a pull request that fixes this issue: <a href="https://github.com/ruby/ruby/pull/3306" class="external">https://github.com/ruby/ruby/pull/3306</a></p> Ruby master - Bug #17017: Range#max & Range#minmax incorrectly use Float end as maxhttps://bugs.ruby-lang.org/issues/17017?journal_id=865322020-07-13T17:09:58Zjeremyevans (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="Fix Range#{max,minmax} for range with integer beginning and non-integer end Previously, for incl..." href="https://bugs.ruby-lang.org/projects/ruby-master/repository/git/revisions/8900a25581822759daca528d46a75e0b743fc22e">git|8900a25581822759daca528d46a75e0b743fc22e</a>.</p>
<hr>
<p>Fix Range#{max,minmax} for range with integer beginning and non-integer end</p>
<p>Previously, for inclusive ranges, the max would show up as the<br>
end of the range, even though the end was not an integer and would<br>
not be the maximum value. For exclusive ranges, max/minmax would<br>
previously raise a TypeError, even though it is possible to get the<br>
correct maximum.</p>
<p>This change to max/minmax also uncovered a similar error in cover?,<br>
which calls max in certain cases, so adjust the code there so that<br>
cover? still works as expected.</p>
<p>Fixes [Bug <a class="issue tracker-1 status-5 priority-4 priority-default closed" title="Bug: Range#max & Range#minmax incorrectly use Float end as max (Closed)" href="https://bugs.ruby-lang.org/issues/17017">#17017</a>]</p> Ruby master - Bug #17017: Range#max & Range#minmax incorrectly use Float end as maxhttps://bugs.ruby-lang.org/issues/17017?journal_id=865472020-07-15T00:47:19Zkoic (Koichi ITO)koic.ito@gmail.com
<ul><li><strong>ruby -v</strong> changed from <i>2.7.1</i> to <i>ruby 2.8.0dev (2020-07-14T04:19:55Z master e60cd14d85) [x86_64-darwin17]</i></li></ul><p>I encountered a breaking change in RuboCop repository when using ruby 2.8.0dev.<br>
<a href="https://github.com/rubocop-hq/rubocop/blob/v0.88.0/lib/rubocop/comment_config.rb#L110" class="external">https://github.com/rubocop-hq/rubocop/blob/v0.88.0/lib/rubocop/comment_config.rb#L110</a></p>
<p>So, I have a question. Is this an expected behaviour?</p>
<p>Ruby 2.7.1:</p>
<pre><code>% ruby -ve 'p (42..Float::INFINITY).max'
ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-darwin17]
Infinity
</code></pre>
<p>Ruby 2.8.0-dev:</p>
<pre><code>% ruby -ve 'p (42..Float::INFINITY).max'
ruby 2.8.0dev (2020-07-14T04:19:55Z master e60cd14d85) [x86_64-darwin17]
-e:1:in `floor': Infinity (FloatDomainError)
from -e:1:in `max'
from -e:1:in `<main>'
</code></pre>
<p>So, it expects that <code>FloatDomainError</code> will not occur even if the end of range is <code>Float::INFINITY</code>.</p>
<p>Thank you.</p> Ruby master - Bug #17017: Range#max & Range#minmax incorrectly use Float end as maxhttps://bugs.ruby-lang.org/issues/17017?journal_id=865512020-07-15T05:24:48Zjeremyevans0 (Jeremy Evans)merch-redmine@jeremyevans.net
<ul></ul><p>koic (Koichi ITO) wrote in <a href="#note-6">#note-6</a>:</p>
<blockquote>
<p>I encountered a breaking change in RuboCop repository when using ruby 2.8.0dev.<br>
<a href="https://github.com/rubocop-hq/rubocop/blob/v0.88.0/lib/rubocop/comment_config.rb#L110" class="external">https://github.com/rubocop-hq/rubocop/blob/v0.88.0/lib/rubocop/comment_config.rb#L110</a></p>
<p>So, I have a question. Is this an expected behaviour?</p>
</blockquote>
<p>Yes, it is expected behavior, at least to me. I believe the Ruby 2.7 behavior is wrong, because a range that starts with an integer will never have a non-integer maximum value, since the increment is an integer. Range#max specifies the maximum value in the range (should be the same as range.to_a.max), not the end of the range (that is Range#end). However, my expectation may be different from matz's expectation.</p> Ruby master - Bug #17017: Range#max & Range#minmax incorrectly use Float end as maxhttps://bugs.ruby-lang.org/issues/17017?journal_id=865522020-07-15T07:19:40Zkoic (Koichi ITO)koic.ito@gmail.com
<ul></ul><p>Thank you for your reply. For now I've learned that updating to <code>(42.to_f..Float::INFINITY)</code> will prevent the error.</p> Ruby master - Bug #17017: Range#max & Range#minmax incorrectly use Float end as maxhttps://bugs.ruby-lang.org/issues/17017?journal_id=865572020-07-15T15:37:07Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul><li><strong>Status</strong> changed from <i>Closed</i> to <i>Open</i></li></ul><p>jeremyevans0 (Jeremy Evans) wrote in <a href="#note-7">#note-7</a>:</p>
<blockquote>
<p>koic (Koichi ITO) wrote in <a href="#note-6">#note-6</a>:</p>
<blockquote>
<p>I encountered a breaking change in RuboCop repository when using ruby 2.8.0dev.<br>
<a href="https://github.com/rubocop-hq/rubocop/blob/v0.88.0/lib/rubocop/comment_config.rb#L110" class="external">https://github.com/rubocop-hq/rubocop/blob/v0.88.0/lib/rubocop/comment_config.rb#L110</a></p>
<p>So, I have a question. Is this an expected behaviour?</p>
</blockquote>
<p>Yes, it is expected behavior, at least to me. I believe the Ruby 2.7 behavior is wrong, because a range that starts with an integer will never have a non-integer maximum value, since the increment is an integer.</p>
</blockquote>
<p>I should have been more clear when I commented to take care of Infinity, but I believe that previous behavior should remain. Since there is no <code>Integer::INFINITY</code>, returning <code>Float::INFINITY</code> is quite descriptive and useful. Raising a <code>RangeError</code> doesn't seem helpful. (I wrote <code>RangeError</code> since raising <code>FloatDomainError</code> as currently is definitely a mistake).</p> Ruby master - Bug #17017: Range#max & Range#minmax incorrectly use Float end as maxhttps://bugs.ruby-lang.org/issues/17017?journal_id=865582020-07-15T15:58:57Zjeremyevans0 (Jeremy Evans)merch-redmine@jeremyevans.net
<ul></ul><p>marcandre (Marc-Andre Lafortune) wrote in <a href="#note-9">#note-9</a>:</p>
<blockquote>
<p>jeremyevans0 (Jeremy Evans) wrote in <a href="#note-7">#note-7</a>:</p>
<blockquote>
<p>koic (Koichi ITO) wrote in <a href="#note-6">#note-6</a>:</p>
<blockquote>
<p>I encountered a breaking change in RuboCop repository when using ruby 2.8.0dev.<br>
<a href="https://github.com/rubocop-hq/rubocop/blob/v0.88.0/lib/rubocop/comment_config.rb#L110" class="external">https://github.com/rubocop-hq/rubocop/blob/v0.88.0/lib/rubocop/comment_config.rb#L110</a></p>
<p>So, I have a question. Is this an expected behaviour?</p>
</blockquote>
<p>Yes, it is expected behavior, at least to me. I believe the Ruby 2.7 behavior is wrong, because a range that starts with an integer will never have a non-integer maximum value, since the increment is an integer.</p>
</blockquote>
<p>I should have been more clear when I commented to take care of Infinity, but I believe that previous behavior should remain. Since there is no <code>Integer::INFINITY</code>, returning <code>Float::INFINITY</code> is quite descriptive and useful. Raising a <code>RangeError</code> doesn't seem helpful. (I wrote <code>RangeError</code> since raising <code>FloatDomainError</code> as currently is definitely a mistake).</p>
</blockquote>
<p>With current Ruby, you should use an endless range instead of range with an infinite end.</p>
<p>The previous behavior of returning <code>Float::INFINITY</code> seems wrong to me. I suppose the current behavior of raising <code>FloatDomainError</code> may be unexpected, and <code>RangeError</code> could be returned instead. Note that if you use the exclusive range (<code>42...Float::INFINITY</code>), you would get a <code>TypeError</code> in 2.7.1, so it's not like we exclusively used <code>RangeError</code> for issues like these. Mathematically, <code>42..Float::INFINITY</code> and <code>42...Float::INFINITY</code> represent the same range, so you should get the same behavior for both.</p>
<p>All that said, I'm not completely opposed to special casing <code>Float::INFINITY</code> in this case. I would like to hear opinions from other committers.</p> Ruby master - Bug #17017: Range#max & Range#minmax incorrectly use Float end as maxhttps://bugs.ruby-lang.org/issues/17017?journal_id=865602020-07-15T16:40:35Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul></ul><p>jeremyevans0 (Jeremy Evans) wrote in <a href="#note-10">#note-10</a>:</p>
<blockquote>
<p>With current Ruby, you should use an endless range instead of range with an infinite end.</p>
</blockquote>
<p>Endless ranges are 2.6+. Ruby 2.5 is not yet EOL. RuboCop supports 2.4 and other gems support earlier Rubies.</p>
<p>More importantly, endless range max is currently not useful... Note even <code>(4.2..).max</code> and variations return an infinity. I should open an issue about this...</p>
<blockquote>
<p>Note that if you use the exclusive range (<code>42...Float::INFINITY</code>), you would get a <code>TypeError</code> in 2.7.1, so it's not like we exclusively used <code>RangeError</code> for issues like these.</p>
</blockquote>
<p>I agree with you, <code>TypeError</code> is also wrong.</p>
<blockquote>
<p>Mathematically, <code>42..Float::INFINITY</code> and <code>42...Float::INFINITY</code> represent the same range</p>
</blockquote>
<p>We'll have to disagree with this 😅. I'm sure in some mathematical definition that it is the case, but in many others it is not. <code>42..Float::INFINITY</code> is not defined in traditional Euclidian space. On a Riemann sphere it is not the same as <code>42...Float::INFINITY</code>. If seen as sequences, you could see them as ω ("infinity") and ω - 1 which are distinct numbers (see surreal numbers), etc.</p>
<blockquote>
<p>All that said, I'm not completely opposed to special casing <code>Float::INFINITY</code> in this case.</p>
</blockquote>
<p>Great. I hope we can make infinity handling useful.</p> Ruby master - Bug #17017: Range#max & Range#minmax incorrectly use Float end as maxhttps://bugs.ruby-lang.org/issues/17017?journal_id=865712020-07-16T09:31:57Zyahonda (Yasuo Honda)yasuo.honda@gmail.com
<ul></ul><p>Let me provide another incompatibility/breaking change since this commit.</p>
<p>One of the Rails validation, <code>validates_length_of(:title, within: 5..Float::INFINITY)</code> raises <code>Infinity (FloatDomainError)</code> with Ruby 2.8.0dev.<br>
This behavior of <code>validates_length_of</code> implemented since Rails 4.0.0 by <a href="https://github.com/rails/rails/pull/4905" class="external">https://github.com/rails/rails/pull/4905</a></p>
<p>I think there is no "Integer::INFINITY" in Ruby, "..Float::INFINITY" may be used by other applications/frameworks.<br>
It would be appreciated that this Rails behavior kept with Ruby 2.8.0-dev.</p>
<ul>
<li>Script to reproduce</li>
</ul>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="c1"># frozen_string_literal: true</span>
<span class="nb">require</span> <span class="s2">"bundler/inline"</span>
<span class="n">gemfile</span><span class="p">(</span><span class="kp">true</span><span class="p">)</span> <span class="k">do</span>
<span class="n">source</span> <span class="s2">"https://rubygems.org"</span>
<span class="n">git_source</span><span class="p">(</span><span class="ss">:github</span><span class="p">)</span> <span class="p">{</span> <span class="o">|</span><span class="n">repo</span><span class="o">|</span> <span class="s2">"https://github.com/</span><span class="si">#{</span><span class="n">repo</span><span class="si">}</span><span class="s2">.git"</span> <span class="p">}</span>
<span class="n">gem</span> <span class="s2">"rails"</span><span class="p">,</span> <span class="ss">github: </span><span class="s2">"rails/rails"</span>
<span class="n">gem</span> <span class="s2">"sqlite3"</span>
<span class="k">end</span>
<span class="nb">require</span> <span class="s2">"active_record"</span>
<span class="nb">require</span> <span class="s2">"minitest/autorun"</span>
<span class="nb">require</span> <span class="s2">"logger"</span>
<span class="c1"># This connection will do for database-independent bug reports.</span>
<span class="no">ActiveRecord</span><span class="o">::</span><span class="no">Base</span><span class="p">.</span><span class="nf">establish_connection</span><span class="p">(</span><span class="ss">adapter: </span><span class="s2">"sqlite3"</span><span class="p">,</span> <span class="ss">database: </span><span class="s2">":memory:"</span><span class="p">)</span>
<span class="no">ActiveRecord</span><span class="o">::</span><span class="no">Base</span><span class="p">.</span><span class="nf">logger</span> <span class="o">=</span> <span class="no">Logger</span><span class="p">.</span><span class="nf">new</span><span class="p">(</span><span class="no">STDOUT</span><span class="p">)</span>
<span class="no">ActiveRecord</span><span class="o">::</span><span class="no">Schema</span><span class="p">.</span><span class="nf">define</span> <span class="k">do</span>
<span class="n">create_table</span> <span class="ss">:posts</span><span class="p">,</span> <span class="ss">force: </span><span class="kp">true</span> <span class="k">do</span> <span class="o">|</span><span class="n">t</span><span class="o">|</span>
<span class="n">t</span><span class="p">.</span><span class="nf">string</span> <span class="ss">:title</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">class</span> <span class="nc">Post</span> <span class="o"><</span> <span class="no">ActiveRecord</span><span class="o">::</span><span class="no">Base</span>
<span class="n">validates_length_of</span><span class="p">(</span><span class="ss">:title</span><span class="p">,</span> <span class="ss">within: </span><span class="mi">5</span><span class="o">..</span><span class="no">Float</span><span class="o">::</span><span class="no">INFINITY</span><span class="p">)</span>
<span class="k">end</span>
<span class="k">class</span> <span class="nc">ValidationTest</span> <span class="o"><</span> <span class="no">Minitest</span><span class="o">::</span><span class="no">Test</span>
<span class="k">def</span> <span class="nf">test_validates_length_of</span>
<span class="n">post</span> <span class="o">=</span> <span class="no">Post</span><span class="p">.</span><span class="nf">create!</span><span class="p">(</span><span class="ss">title: </span><span class="s2">"first post"</span><span class="p">)</span>
<span class="n">assert</span> <span class="n">post</span><span class="p">.</span><span class="nf">title</span><span class="p">,</span> <span class="s2">"first post"</span>
<span class="k">end</span>
<span class="k">end</span>
</code></pre>
<ul>
<li>Result with ruby 2.8.0dev raises "Infinity (FloatDomainError)"</li>
</ul>
<pre><code>% ruby -v ; ruby diag.rb
ruby 2.8.0dev (2020-07-16T02:49:09Z master 1fb4e28002) [x86_64-darwin20]
Fetching https://github.com/rails/rails.git
Fetching gem metadata from https://rubygems.org/......
Fetching gem metadata from https://rubygems.org/............
Fetching gem metadata from https://rubygems.org/............
Resolving dependencies...
Using rake 13.0.1
Using bundler 2.2.0.dev
Using method_source 1.0.0
Using minitest 5.14.1
Using zeitwerk 2.4.0
Using builder 3.2.4
Using erubi 1.9.0
Using mini_portile2 2.4.0
Using crass 1.0.6
Using rack 2.2.3
Using nio4r 2.5.2
Using websocket-extensions 0.1.5
Using mimemagic 0.3.5
Using mini_mime 1.0.2
Using marcel 0.3.3
Using sqlite3 1.4.2
Using concurrent-ruby 1.1.6
Using nokogiri 1.10.10
Using rack-test 1.1.0
Using websocket-driver 0.7.3
Using thor 1.0.1
Using mail 2.7.1
Using i18n 1.8.3
Using tzinfo 2.0.2
Using sprockets 4.0.2
Using loofah 2.6.0
Using activesupport 6.1.0.alpha from https://github.com/rails/rails.git (at master@bcfa51f)
Using rails-html-sanitizer 1.3.0
Using rails-dom-testing 2.0.3
Using globalid 0.4.2
Using activemodel 6.1.0.alpha from https://github.com/rails/rails.git (at master@bcfa51f)
Using actionview 6.1.0.alpha from https://github.com/rails/rails.git (at master@bcfa51f)
Using activejob 6.1.0.alpha from https://github.com/rails/rails.git (at master@bcfa51f)
Using actionpack 6.1.0.alpha from https://github.com/rails/rails.git (at master@bcfa51f)
Using activerecord 6.1.0.alpha from https://github.com/rails/rails.git (at master@bcfa51f)
Using actioncable 6.1.0.alpha from https://github.com/rails/rails.git (at master@bcfa51f)
Using activestorage 6.1.0.alpha from https://github.com/rails/rails.git (at master@bcfa51f)
Using railties 6.1.0.alpha from https://github.com/rails/rails.git (at master@bcfa51f)
Using sprockets-rails 3.2.1
Using actionmailer 6.1.0.alpha from https://github.com/rails/rails.git (at master@bcfa51f)
Using actionmailbox 6.1.0.alpha from https://github.com/rails/rails.git (at master@bcfa51f)
Using actiontext 6.1.0.alpha from https://github.com/rails/rails.git (at master@bcfa51f)
Using rails 6.1.0.alpha from https://github.com/rails/rails.git (at master@bcfa51f)
-- create_table(:posts, {:force=>true})
D, [2020-07-16T18:20:56.790796 #66220] DEBUG -- : (0.7ms) SELECT sqlite_version(*)
D, [2020-07-16T18:20:56.791115 #66220] DEBUG -- : (0.0ms) DROP TABLE IF EXISTS "posts"
D, [2020-07-16T18:20:56.791443 #66220] DEBUG -- : (0.2ms) CREATE TABLE "posts" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar)
-> 0.0038s
D, [2020-07-16T18:20:56.814223 #66220] DEBUG -- : (0.1ms) CREATE TABLE "ar_internal_metadata" ("key" varchar NOT NULL PRIMARY KEY, "value" varchar, "created_at" datetime(6) NOT NULL, "updated_at" datetime(6) NOT NULL)
D, [2020-07-16T18:20:56.824002 #66220] DEBUG -- : ActiveRecord::InternalMetadata Load (0.1ms) SELECT "ar_internal_metadata".* FROM "ar_internal_metadata" WHERE "ar_internal_metadata"."key" = ? LIMIT ? [["key", "environment"], ["LIMIT", 1]]
D, [2020-07-16T18:20:56.827703 #66220] DEBUG -- : TRANSACTION (0.0ms) begin transaction
D, [2020-07-16T18:20:56.827989 #66220] DEBUG -- : ActiveRecord::InternalMetadata Create (0.1ms) INSERT INTO "ar_internal_metadata" ("key", "value", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["key", "environment"], ["value", "development"], ["created_at", "2020-07-16 09:20:56.827304"], ["updated_at", "2020-07-16 09:20:56.827304"]]
D, [2020-07-16T18:20:56.828184 #66220] DEBUG -- : TRANSACTION (0.0ms) commit transaction
/Users/yahonda/.rbenv/versions/2.8.0-dev/lib/ruby/gems/2.8.0/bundler/gems/rails-bcfa51fba6ee/activemodel/lib/active_model/validations/length.rb:14:in `floor': Infinity (FloatDomainError)
from /Users/yahonda/.rbenv/versions/2.8.0-dev/lib/ruby/gems/2.8.0/bundler/gems/rails-bcfa51fba6ee/activemodel/lib/active_model/validations/length.rb:14:in `max'
from /Users/yahonda/.rbenv/versions/2.8.0-dev/lib/ruby/gems/2.8.0/bundler/gems/rails-bcfa51fba6ee/activemodel/lib/active_model/validations/length.rb:14:in `initialize'
from /Users/yahonda/.rbenv/versions/2.8.0-dev/lib/ruby/gems/2.8.0/bundler/gems/rails-bcfa51fba6ee/activemodel/lib/active_model/validations/with.rb:86:in `new'
from /Users/yahonda/.rbenv/versions/2.8.0-dev/lib/ruby/gems/2.8.0/bundler/gems/rails-bcfa51fba6ee/activemodel/lib/active_model/validations/with.rb:86:in `block in validates_with'
from /Users/yahonda/.rbenv/versions/2.8.0-dev/lib/ruby/gems/2.8.0/bundler/gems/rails-bcfa51fba6ee/activemodel/lib/active_model/validations/with.rb:85:in `each'
from /Users/yahonda/.rbenv/versions/2.8.0-dev/lib/ruby/gems/2.8.0/bundler/gems/rails-bcfa51fba6ee/activemodel/lib/active_model/validations/with.rb:85:in `validates_with'
from /Users/yahonda/.rbenv/versions/2.8.0-dev/lib/ruby/gems/2.8.0/bundler/gems/rails-bcfa51fba6ee/activerecord/lib/active_record/validations/length.rb:20:in `validates_length_of'
from diag.rb:29:in `<class:Post>'
from diag.rb:28:in `<main>'
%
</code></pre> Ruby master - Bug #17017: Range#max & Range#minmax incorrectly use Float end as maxhttps://bugs.ruby-lang.org/issues/17017?journal_id=865742020-07-16T17:17:44Zjeremyevans0 (Jeremy Evans)merch-redmine@jeremyevans.net
<ul></ul><p>I've added a pull request to restore compatibility for Range#max with integer beginning and Float::Infinity end: <a href="https://github.com/ruby/ruby/pull/3326" class="external">https://github.com/ruby/ruby/pull/3326</a></p>
<p>I think this pull request should require matz approval because it is a deliberate tradeoff of correctness in order to keep compatibility. The correct behavior should be raising RangeError, as that is what an endless range returns. If you would like matz to consider it, please bring it up for discussion at a future Ruby developers meeting.</p> Ruby master - Bug #17017: Range#max & Range#minmax incorrectly use Float end as maxhttps://bugs.ruby-lang.org/issues/17017?journal_id=865972020-07-19T01:17:30Zyahonda (Yasuo Honda)yasuo.honda@gmail.com
<ul></ul><p>I have confirmed <a href="https://github.com/ruby/ruby/pull/3326" class="external">https://github.com/ruby/ruby/pull/3326</a> addresses the Active Model failure reported at <a href="https://bugs.ruby-lang.org/issues/17017#note-12" class="external">https://bugs.ruby-lang.org/issues/17017#note-12</a></p> Ruby master - Bug #17017: Range#max & Range#minmax incorrectly use Float end as maxhttps://bugs.ruby-lang.org/issues/17017?journal_id=866032020-07-19T06:41:43Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul></ul><p>jeremyevans0 (Jeremy Evans) wrote in <a href="#note-13">#note-13</a>:</p>
<blockquote>
<p>I've added a pull request to restore compatibility for Range#max with integer beginning and Float::Infinity end: <a href="https://github.com/ruby/ruby/pull/3326" class="external">https://github.com/ruby/ruby/pull/3326</a></p>
</blockquote>
<p>Good, thanks.</p>
<blockquote>
<p>I think this pull request should require matz approval because it is a deliberate tradeoff of correctness in order to keep compatibility. The correct behavior should be raising RangeError, as that is what an endless range returns.</p>
</blockquote>
<p>Well, <code>(1.0..).max</code> raises a <code>RangeError</code> while <code>(1.0..Float::INFINITY).max</code> returns <code>Float::INFINITY</code>; which is "correct"?</p>
<blockquote>
<p>If you would like matz to consider it, please bring it up for discussion at a future Ruby developers meeting.</p>
</blockquote>
<p>I'm surprised by the lack of response from other Ruby committers.</p>
<p>Unless I'm mistaken, this behavior change was not approved by Matz (or anybody else), changes a behavior that dates back to Ruby 1.8, breaks (that we know of) <code>activemodel</code> and <code>rubocop</code>, doesn't even raise the right error and isn't tested, and is a change that I and others disapprove of. Yet when you propose that that commit remains as is and that someone else has to bring up the situation with Matz, no other committer seems confused by this.</p>
<p>I would love to have pointers on what I'm doing wrong. I've had a commit reverted twice in a row because a committer disagreed with me on a much smaller and obscure bug fix. I've endured verbal abuse. I've been reprimanded for not waiting to get the approval from Matz for committing an improvement to the wording of a warning (even though nobody thought the updated warning had any issue with it, or that it could potentially break any code out there). I envy you, and I really would like to understand 😅</p> Ruby master - Bug #17017: Range#max & Range#minmax incorrectly use Float end as maxhttps://bugs.ruby-lang.org/issues/17017?journal_id=866042020-07-19T09:59:45ZEregon (Benoit Daloze)
<ul></ul><p>marcandre (Marc-Andre Lafortune) wrote in <a href="#note-15">#note-15</a>:</p>
<blockquote>
<p>Unless I'm mistaken, this behavior change was not approved by Matz (or anybody else), changes a behavior that dates back to Ruby 1.8, breaks (that we know of) <code>activemodel</code> and <code>rubocop</code>, doesn't even raise the right error and isn't tested, ...</p>
</blockquote>
<p>Which change is that?<br>
The first PR above, <a href="https://github.com/ruby/ruby/pull/3306" class="external">https://github.com/ruby/ruby/pull/3306</a> ?<br>
That seems to have some tests.</p>
<blockquote>
<p>Yet when you propose that that commit remains as is and that someone else has to bring up the situation with Matz, no other committer seems confused by this.</p>
</blockquote>
<p>I think the first PR should already have matz's or at least someone's else review.</p>
<p>Range is full of edge and special cases though, and there seems to be no global vision for it.<br>
Maybe a good step here would be try to define the semantics we'd like for it. Obviously it's quite some work.</p>
<p>If reverting the first PR helps, maybe we should do that first?</p> Ruby master - Bug #17017: Range#max & Range#minmax incorrectly use Float end as maxhttps://bugs.ruby-lang.org/issues/17017?journal_id=866052020-07-19T10:02:20ZEregon (Benoit Daloze)
<ul></ul><p>BTW I think we should rewrite Range#max and others in pure Ruby.<br>
It would be so much easier to read and discuss the semantics.<br>
I find <code>range_max()</code> is very hard to read.</p> Ruby master - Bug #17017: Range#max & Range#minmax incorrectly use Float end as maxhttps://bugs.ruby-lang.org/issues/17017?journal_id=866062020-07-19T12:38:51Zjeremyevans0 (Jeremy Evans)merch-redmine@jeremyevans.net
<ul></ul><p>marcandre (Marc-Andre Lafortune) wrote in <a href="#note-15">#note-15</a>:</p>
<blockquote>
<p>jeremyevans0 (Jeremy Evans) wrote in <a href="#note-13">#note-13</a>:</p>
<blockquote>
<p>I think this pull request should require matz approval because it is a deliberate tradeoff of correctness in order to keep compatibility. The correct behavior should be raising RangeError, as that is what an endless range returns.</p>
</blockquote>
<p>Well, <code>(1.0..).max</code> raises a <code>RangeError</code> while <code>(1.0..Float::INFINITY).max</code> returns <code>Float::INFINITY</code>; which is "correct"?</p>
</blockquote>
<p>Range#max behavior for integer ranges is different than behavior for float ranges. <code>(1..2.1).max</code> and <code>(1.0..2.1).max</code> have different result even though <code>1 == 1.0</code>. However, I see your point, that there is precedent for behavior to differ between endless ranges and ranges with infinite end.</p>
<blockquote>
<p>Unless I'm mistaken, this behavior change was not approved by Matz (or anybody else), changes a behavior that dates back to Ruby 1.8, breaks (that we know of) <code>activemodel</code> and <code>rubocop</code>, doesn't even raise the right error and isn't tested, and is a change that I and others disapprove of. Yet when you propose that that commit remains as is and that someone else has to bring up the situation with Matz, no other committer seems confused by this.</p>
</blockquote>
<p>The behavior change in this issue is to fix an obvious bug, which is that <code>(1..2.1).max</code> returned <code>2.1</code> instead of <code>2</code>. In general we don't require Matz's approval to fix obvious bugs. Were you against fixing that bug?</p>
<p>After the change was committed, that is when there were reports that the <code>(1..Float::Infinity).max</code> case broke. The only reason that case worked previously is it was relying on the previous bug (that Range#max returned a float end for an integer range). It's not like I removed a special case for it. I even worked on a pull request to add a special case for it for compatibility. However, since I think it makes Range#max behavior inconsistent for integer ranges, I think it is something that should be approved by Matz. Now, I may be wrong here. As committers go, I'm fairly junior. If another committer with more experience thinks it something that can be committed without approval from Matz, they can merge my pull request.</p> Ruby master - Bug #17017: Range#max & Range#minmax incorrectly use Float end as maxhttps://bugs.ruby-lang.org/issues/17017?journal_id=866072020-07-19T14:12:54Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul></ul><p>Eregon (Benoit Daloze) wrote in <a href="#note-16">#note-16</a>:</p>
<blockquote>
<p>marcandre (Marc-Andre Lafortune) wrote in <a href="#note-15">#note-15</a>:</p>
<blockquote>
<p>Unless I'm mistaken, this behavior change was not approved by Matz (or anybody else), changes a behavior that dates back to Ruby 1.8, breaks (that we know of) <code>activemodel</code> and <code>rubocop</code>, doesn't even raise the right error and isn't tested, ...</p>
</blockquote>
<p>Which change is that?</p>
</blockquote>
<p>I should have been more clear: by "this behavior change" I was referring exclusively to the change to <code>(1..Float::INFINITY).max</code>.</p>
<p>jeremyevans0 (Jeremy Evans) wrote in <a href="#note-13">#note-13</a>:</p>
<blockquote>
<p>...obvious bug, which is that (1..2.1).max returned 2.1 instead of 2...<br>
Were you against fixing that bug?</p>
</blockquote>
<p>Sorry I wasn't clear: I am not against that fix.</p> Ruby master - Bug #17017: Range#max & Range#minmax incorrectly use Float end as maxhttps://bugs.ruby-lang.org/issues/17017?journal_id=866082020-07-19T14:19:40Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul></ul><p>jeremyevans0 (Jeremy Evans) wrote in <a href="#note-18">#note-18</a>:</p>
<blockquote>
<p>If another committer with more experience thinks it something that can be committed without approval from Matz, they can merge my pull request.</p>
</blockquote>
<p>Good, so since you don't mind, I will merge that PR that reinstates the previous behavior (again, to be clear, about how to treat <code>Float::INFINITY</code> end) that is both useful and relied upon, and we can discuss the proper behavior for <code>(1.0..).max</code> and other variants. 👍</p> Ruby master - Bug #17017: Range#max & Range#minmax incorrectly use Float end as maxhttps://bugs.ruby-lang.org/issues/17017?journal_id=866092020-07-19T14:27:00Zjeremyevans (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="Special case Range#max for integer beginning and Float::Infinity end Popular Ruby libraries such..." href="https://bugs.ruby-lang.org/projects/ruby-master/repository/git/revisions/05bf811c2839628aaef3d565daedb28be80d47ef">git|05bf811c2839628aaef3d565daedb28be80d47ef</a>.</p>
<hr>
<p>Special case Range#max for integer beginning and Float::Infinity end</p>
<p>Popular Ruby libraries such as Rails and Rubocop relying on the<br>
previous behavior, even though it is technically a bug. The<br>
correct behavior is probably raising RangeError, since that is what<br>
an endless range raises.</p>
<p>Related to [Bug <a class="issue tracker-1 status-5 priority-4 priority-default closed" title="Bug: Range#max & Range#minmax incorrectly use Float end as max (Closed)" href="https://bugs.ruby-lang.org/issues/17017">#17017</a>]</p> Ruby master - Bug #17017: Range#max & Range#minmax incorrectly use Float end as maxhttps://bugs.ruby-lang.org/issues/17017?journal_id=870662020-08-14T16:09:05ZDan0042 (Daniel DeLorme)
<ul></ul><p>jeremyevans0 (Jeremy Evans) wrote in <a href="#note-18">#note-18</a>:</p>
<blockquote>
<p>The behavior change in this issue is to fix an obvious bug, which is that <code>(1..2.1).max</code> returned <code>2.1</code> instead of <code>2</code>.</p>
</blockquote>
<p>FWIW I consider the previous behavior correct. Intuitively I see <code>1..2.1</code> as a float range because one of the ends is a float. And so I expect <code>(1..2.1).max</code> to be equivalent to <code>(1.0..2.1).max</code></p> Ruby master - Bug #17017: Range#max & Range#minmax incorrectly use Float end as maxhttps://bugs.ruby-lang.org/issues/17017?journal_id=870672020-08-14T17:54:06Zjeremyevans0 (Jeremy Evans)merch-redmine@jeremyevans.net
<ul></ul><p>Dan0042 (Daniel DeLorme) wrote in <a href="#note-22">#note-22</a>:</p>
<blockquote>
<p>jeremyevans0 (Jeremy Evans) wrote in <a href="#note-18">#note-18</a>:</p>
<blockquote>
<p>The behavior change in this issue is to fix an obvious bug, which is that <code>(1..2.1).max</code> returned <code>2.1</code> instead of <code>2</code>.</p>
</blockquote>
<p>FWIW I consider the previous behavior correct. Intuitively I see <code>1..2.1</code> as a float range because one of the ends is a float. And so I expect <code>(1..2.1).max</code> to be equivalent to <code>(1.0..2.1).max</code></p>
</blockquote>
<p><code>integer..float</code> is currently treated as a integer range in all other respects. For example:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="p">(</span><span class="mf">1.0</span><span class="o">..</span><span class="mf">2.1</span><span class="p">).</span><span class="nf">to_a</span> <span class="c1"># TypeError (can't iterate from Float)</span>
<span class="p">(</span><span class="mi">1</span><span class="o">..</span><span class="mf">2.1</span><span class="p">).</span><span class="nf">to_a</span> <span class="c1"># => [1, 2]</span>
</code></pre>
<p>So if we want to treat <code>integer..float</code> to be a float range, it will require changes far beyond <code>#max</code> and <code>#minmax</code>.</p>
<p>I think this is a fix for a bug/regression introduced in Ruby 1.9 due to an improper optimization:</p>
<pre><code>$ ruby18 -ve 'p((1..2.1).max)'
ruby 1.8.7 (2013-06-27 patchlevel 374) [x86_64-openbsd]
2
$ ruby19 -ve 'p((1..2.1).max)'
ruby 1.9.3p551 (2014-11-13 revision 48407) [x86_64-openbsd]
2.1
</code></pre> Ruby master - Bug #17017: Range#max & Range#minmax incorrectly use Float end as maxhttps://bugs.ruby-lang.org/issues/17017?journal_id=873352020-09-01T05:13:22Zmatz (Yukihiro Matsumoto)matz@ruby.or.jp
<ul></ul><p><code>Range</code> class methods are classified in two. The ones that behave like <code>Enumerable</code> methods (defined by <code>#each</code>), and the others that behave like region (defined by the both ends).<br>
I think <code>#min</code> and <code>#max</code> should belong to the latter. So the old behavior is preferable, that is <code>(1..3.5).max #=> 3.5</code>.</p>
<p>Matz.</p> Ruby master - Bug #17017: Range#max & Range#minmax incorrectly use Float end as maxhttps://bugs.ruby-lang.org/issues/17017?journal_id=873392020-09-01T14:49:03Zjeremyevans0 (Jeremy Evans)merch-redmine@jeremyevans.net
<ul></ul><p>matz (Yukihiro Matsumoto) wrote in <a href="#note-24">#note-24</a>:</p>
<blockquote>
<p><code>Range</code> class methods are classified in two. The ones that behave like <code>Enumerable</code> methods (defined by <code>#each</code>), and the others that behave like region (defined by the both ends).<br>
I think <code>#min</code> and <code>#max</code> should belong to the latter. So the old behavior is preferable, that is <code>(1..3.5).max #=> 3.5</code>.</p>
</blockquote>
<p>OK, I'll revert the related commits and also update the documentation to clarify the behavior.</p>
<p>Note that the previous behavior for exclusive integer ranges was like Enumerable and not like region (<code>(1...3).max #=> 2</code>, even though <code>(1...3).include?(2.9) #=> true</code>). I assume that the previous behavior for that should be kept, even if it is inconsistent.</p> Ruby master - Bug #17017: Range#max & Range#minmax incorrectly use Float end as maxhttps://bugs.ruby-lang.org/issues/17017?journal_id=873462020-09-01T17:53:59Zjeremyevans0 (Jeremy Evans)merch-redmine@jeremyevans.net
<ul></ul><p>I reverted the changes in this pull request, and updated the Range#max documentation to explain the behavior (<a class="changeset" title="Update documentation for Range#max" href="https://bugs.ruby-lang.org/projects/ruby-master/repository/git/revisions/de10a1f3583adeeffd7f8bcf8f276e0a626ffa0c">de10a1f3583adeeffd7f8bcf8f276e0a626ffa0c</a>).</p>