[前][次][番号順一覧][スレッド一覧]

ruby-changes:62266

From: Michael <ko1@a...>
Date: Sun, 19 Jul 2020 01:18:57 +0900 (JST)
Subject: [ruby-changes:62266] 8a5ad2b77d (master): Fix Range#max for beginless Integer ranges [Bug #17034]

https://git.ruby-lang.org/ruby.git/commit/?id=8a5ad2b77d

From 8a5ad2b77d7a24e4f8f4fef179ae5efced935f91 Mon Sep 17 00:00:00 2001
From: Michael Kohl <me@c...>
Date: Sat, 18 Jul 2020 23:18:40 +0700
Subject: Fix Range#max for beginless Integer ranges [Bug #17034]

* Fix Range#max for beginless Integer ranges
* Update test/ruby/test_range.rb
* Fix formatting

https://github.com/ruby/ruby/pull/3328

Co-authored-by: Nobuyoshi Nakada <nobu@r...>

diff --git a/range.c b/range.c
index 93cf126..124d9fa 100644
--- a/range.c
+++ b/range.c
@@ -1220,16 +1220,17 @@ range_max(int argc, VALUE *argv, VALUE range) https://github.com/ruby/ruby/blob/trunk/range.c#L1220
 	rb_raise(rb_eRangeError, "cannot get the maximum of endless range");
     }
 
+    VALUE b = RANGE_BEG(range);
+
     if (rb_block_given_p() || (EXCL(range) && !nm) || argc) {
-        if (NIL_P(RANGE_BEG(range))) {
+        if (NIL_P(b)) {
             rb_raise(rb_eRangeError, "cannot get the maximum of beginless range with custom comparison method");
         }
         return rb_call_super(argc, argv);
     }
     else {
         struct cmp_opt_data cmp_opt = { 0, 0 };
-        VALUE b = RANGE_BEG(range);
-        int c = OPTIMIZED_CMP(b, e, cmp_opt);
+        int c = NIL_P(b) ? -1 : OPTIMIZED_CMP(b, e, cmp_opt);
 
         if (c > 0)
             return Qnil;
diff --git a/test/ruby/test_range.rb b/test/ruby/test_range.rb
index 9052fe6..0b3f6c6 100644
--- a/test/ruby/test_range.rb
+++ b/test/ruby/test_range.rb
@@ -127,6 +127,10 @@ class TestRange < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_range.rb#L127
     assert_raise(RangeError) { (1...).max(3) }
 
     assert_raise(RangeError) { (..0).min {|a, b| a <=> b } }
+
+    assert_equal(2, (..2).max)
+    assert_raise(TypeError) { (...2).max }
+    assert_raise(TypeError) { (...2.0).max }
   end
 
   def test_minmax
-- 
cgit v0.10.2


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]