Project

General

Profile

Actions

Bug #13266

closed

Slicing in array producing wrong results

Added by aman@ruby (Aman Puri) about 7 years ago. Updated about 7 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
[ruby-core:79854]

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
Actions

Also available in: Atom PDF

Like0
Like0Like0