Project

General

Profile

Bug #17034 ยป range-max-beginless.patch

citizen428 (Michael Kohl), 07/17/2020 04:25 PM

View differences:

range.c
1220 1220
	rb_raise(rb_eRangeError, "cannot get the maximum of endless range");
1221 1221
    }
1222 1222

  
1223
    VALUE b = RANGE_BEG(range);
1224

  
1223 1225
    if (rb_block_given_p() || (EXCL(range) && !nm) || argc) {
1224
        if (NIL_P(RANGE_BEG(range))) {
1226
        if (NIL_P(b)) {
1225 1227
            rb_raise(rb_eRangeError, "cannot get the maximum of beginless range with custom comparison method");
1226 1228
        }
1227 1229
        return rb_call_super(argc, argv);
1228 1230
    }
1229 1231
    else {
1230 1232
        struct cmp_opt_data cmp_opt = { 0, 0 };
1231
        VALUE b = RANGE_BEG(range);
1232
        int c = OPTIMIZED_CMP(b, e, cmp_opt);
1233
        int c = NIL_P(b) ? -1 : OPTIMIZED_CMP(b, e, cmp_opt);
1233 1234

  
1234 1235
        if (c > 0)
1235 1236
            return Qnil;
test/ruby/test_range.rb
127 127
    assert_raise(RangeError) { (1...).max(3) }
128 128

  
129 129
    assert_raise(RangeError) { (..0).min {|a, b| a <=> b } }
130

  
131
    assert_equal(2, (..2).max)
132
    assert_raise(TypeError, (...2).max)
133
    assert_raise(TypeError, (...2.0).max)
130 134
  end
131 135

  
132 136
  def test_minmax