ruby-changes:59475
From: Yusuke <ko1@a...>
Date: Wed, 25 Dec 2019 13:41:46 +0900 (JST)
Subject: [ruby-changes:59475] 81e377023c (master): range.c: Range#min with a beginless one now raise an explicit exception
https://git.ruby-lang.org/ruby.git/commit/?id=81e377023c From 81e377023c490998a3fec245ca2fb2b3c710c2c6 Mon Sep 17 00:00:00 2001 From: Yusuke Endoh <mame@r...> Date: Wed, 25 Dec 2019 13:35:22 +0900 Subject: range.c: Range#min with a beginless one now raise an explicit exception [Bug #16450] diff --git a/range.c b/range.c index fe95619..bf14c0c 100644 --- a/range.c +++ b/range.c @@ -1136,6 +1136,10 @@ range_last(int argc, VALUE *argv, VALUE range) https://github.com/ruby/ruby/blob/trunk/range.c#L1136 static VALUE range_min(int argc, VALUE *argv, VALUE range) { + if (NIL_P(RANGE_BEG(range))) { + rb_raise(rb_eRangeError, "cannot get the minimum of beginless 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"); @@ -1185,6 +1189,9 @@ range_max(int argc, VALUE *argv, VALUE range) https://github.com/ruby/ruby/blob/trunk/range.c#L1189 } if (rb_block_given_p() || (EXCL(range) && !nm) || argc) { + if (NIL_P(RANGE_BEG(range))) { + rb_raise(rb_eRangeError, "cannot get the maximum of beginless range with custom comparison method"); + } return rb_call_super(argc, argv); } else { diff --git a/test/ruby/test_range.rb b/test/ruby/test_range.rb index 800cee9..b37dbbc 100644 --- a/test/ruby/test_range.rb +++ b/test/ruby/test_range.rb @@ -81,6 +81,8 @@ class TestRange < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_range.rb#L81 assert_equal(nil, (2..1).min) assert_equal(1, (1...2).min) assert_equal(1, (1..).min) + assert_raise(RangeError) { (..1).min } + assert_raise(RangeError) { (...1).min } assert_equal(1.0, (1.0..2.0).min) assert_equal(nil, (2.0..1.0).min) @@ -93,6 +95,8 @@ class TestRange < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_range.rb#L95 assert_equal([0,1,2], (0..10).min(3)) assert_equal([0,1], (0..1).min(3)) assert_equal([0,1,2], (0..).min(3)) + assert_raise(RangeError) { (..1).min(3) } + assert_raise(RangeError) { (...1).min(3) } assert_raise(RangeError) { (0..).min {|a, b| a <=> b } } end @@ -119,6 +123,8 @@ class TestRange < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_range.rb#L123 assert_equal([9,8,7], (0...10).max(3)) assert_raise(RangeError) { (1..).max(3) } assert_raise(RangeError) { (1...).max(3) } + + assert_raise(RangeError) { (..0).min {|a, b| a <=> b } } end def test_minmax -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/