Actions
Bug #13266
closedSlicing in array producing wrong results
Description
even_nums = [2,4,6,8,10]
2.3.1 :007 > even_nums[5,0]
=> []
# it must return nil as index is out of bound when slicing
# it may be due to the code written in ruby.c
=begin
if (argc == 2) {
if (SYMBOL_P(argv[0])) {
rb_raise(rb_eTypeError, "Symbol as array index");
}
beg = NUM2LONG(argv[0]);
len = NUM2LONG(argv[1]);
if (beg < 0) {
beg += RARRAY(ary)->len;
}
return rb_ary_subseq(ary, beg, len);
}
=end
# beg will store 5 in it and then this value is compared with RARRAY_LEN(ary)
# in code it is defined that if (beg > RARRAY_LEN(ary)) return Qnil;
# it must be corrected and checked with the index of the array rather then size of array
2.3.1 :008 > even_nums[6,0]
=> nil
Updated by aman@ruby (Aman Puri) about 7 years ago
- ruby -v set to 2.3.1
Updated by nobu (Nobuyoshi Nakada) about 7 years ago
- Description updated (diff)
- Status changed from Open to Rejected
Additionally, an empty array is returned when the starting index for
an element range is at the end of the array.
Actions
Like0
Like0Like0