Project

General

Profile

Bug #14845 ยป range_to_a.patch

mame (Yusuke Endoh), 06/13/2018 11:19 AM

View differences:

range.c
755 755
    return Qnil;
756 756
}
757 757

  
758
/*
759
 *  call-seq:
760
 *     rng.to_a                   -> array
761
 *     rng.entries                -> array
762
 *
763
 *  Returns an array containing the items in <i>rng</i>.
764
 *
765
 *    (1..7).to_a  #=> [1, 2, 3, 4, 5, 6, 7]
766
 *    (1..).to_a   #=> RangeError: cannot convert endless range to an array
767
 */
768

  
769
static VALUE
770
range_to_a(VALUE range)
771
{
772
    if (NIL_P(RANGE_END(range))) {
773
	rb_raise(rb_eRangeError, "cannot convert endless range to an array");
774
    }
775
    return rb_call_super(0, 0);
776
}
777

  
758 778
static VALUE
759 779
range_enum_size(VALUE range, VALUE args, VALUE eobj)
760 780
{
......
1459 1479
    rb_define_method(rb_cRange, "min", range_min, -1);
1460 1480
    rb_define_method(rb_cRange, "max", range_max, -1);
1461 1481
    rb_define_method(rb_cRange, "size", range_size, 0);
1482
    rb_define_method(rb_cRange, "to_a", range_to_a, 0);
1483
    rb_define_method(rb_cRange, "entries", range_to_a, 0);
1462 1484
    rb_define_method(rb_cRange, "to_s", range_to_s, 0);
1463 1485
    rb_define_method(rb_cRange, "inspect", range_inspect, 0);
1464 1486

  
test/ruby/test_range.rb
794 794
    end
795 795
    (a.."c").each {|x, &b| assert_nil(b)}
796 796
  end
797

  
798
  def test_to_a
799
    assert_equal([1,2,3,4,5], (1..5).to_a)
800
    assert_equal([1,2,3,4], (1...5).to_a)
801
    assert_raise(RangeError) { (1..).to_a }
802
  end
797 803
end