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

ruby-changes:51506

From: mame <ko1@a...>
Date: Fri, 22 Jun 2018 11:58:45 +0900 (JST)
Subject: [ruby-changes:51506] mame:r63716 (trunk): range.c: Range#last and #max raises a RangeError if it is endless

mame	2018-06-22 11:58:40 +0900 (Fri, 22 Jun 2018)

  New Revision: 63716

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=63716

  Log:
    range.c: Range#last and #max raises a RangeError if it is endless
    
    Also, Range#min raises an error if it is endless and a comparison method
    is specified.

  Modified files:
    trunk/range.c
    trunk/test/ruby/test_range.rb
Index: test/ruby/test_range.rb
===================================================================
--- test/ruby/test_range.rb	(revision 63715)
+++ test/ruby/test_range.rb	(revision 63716)
@@ -99,8 +99,8 @@ class TestRange < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_range.rb#L99
     assert_equal(2, (1..2).max)
     assert_equal(nil, (2..1).max)
     assert_equal(1, (1...2).max)
-    assert_equal(nil, (1..).max)
-    assert_equal(nil, (1...).max)
+    assert_raise(RangeError) { (1..).max }
+    assert_raise(RangeError) { (1...).max }
 
     assert_equal(2.0, (1.0..2.0).max)
     assert_equal(nil, (2.0..1.0).max)
@@ -115,7 +115,8 @@ class TestRange < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_range.rb#L115
 
     assert_equal([10,9,8], (0..10).max(3))
     assert_equal([9,8,7], (0...10).max(3))
-    # XXX: How should (0...).max(3) behave?
+    assert_raise(RangeError) { (1..).max(3) }
+    assert_raise(RangeError) { (1...).max(3) }
   end
 
   def test_initialize_twice
@@ -405,7 +406,8 @@ class TestRange < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_range.rb#L406
     assert_equal([0, 1, 2], (0..nil).first(3))
     assert_equal(0, (0..nil).first)
     assert_equal("a", ("a"..nil).first)
-    # XXX: How should (0...).last(3) behave?
+    assert_raise(RangeError) { (0..nil).last }
+    assert_raise(RangeError) { (0..nil).last(3) }
   end
 
   def test_to_s
Index: range.c
===================================================================
--- range.c	(revision 63715)
+++ range.c	(revision 63716)
@@ -1013,6 +1013,9 @@ range_first(int argc, VALUE *argv, VALUE https://github.com/ruby/ruby/blob/trunk/range.c#L1013
 static VALUE
 range_last(int argc, VALUE *argv, VALUE range)
 {
+    if (NIL_P(RANGE_END(range))) {
+        rb_raise(rb_eRangeError, "cannot get the last element of endless range");
+    }
     if (argc == 0) return RANGE_END(range);
     return rb_ary_last(argc, argv, rb_Array(range));
 }
@@ -1040,6 +1043,9 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/range.c#L1043
 range_min(int argc, VALUE *argv, VALUE range)
 {
     if (rb_block_given_p()) {
+        if (NIL_P(RANGE_END(range))) {
+            rb_raise(rb_eRangeError, "cannot get the minimum of endless range with custom comparison method");
+        }
 	return rb_call_super(argc, argv);
     }
     else if (argc != 0) {
@@ -1080,7 +1086,9 @@ range_max(int argc, VALUE *argv, VALUE r https://github.com/ruby/ruby/blob/trunk/range.c#L1086
     VALUE e = RANGE_END(range);
     int nm = FIXNUM_P(e) || rb_obj_is_kind_of(e, rb_cNumeric);
 
-    if (NIL_P(e)) return Qnil;
+    if (NIL_P(RANGE_END(range))) {
+	rb_raise(rb_eRangeError, "cannot get the maximum of endless range");
+    }
 
     if (rb_block_given_p() || (EXCL(range) && !nm) || argc) {
         return rb_call_super(argc, argv);

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

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