Issue #13266 has been reported by Aman Puri.

----------------------------------------
Bug #13266: Slicing in array producing wrong results
https://bugs.ruby-lang.org/issues/13266

* Author: Aman Puri
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: 
* Backport: 2.2: UNKNOWN, 2.3: UNKNOWN, 2.4: UNKNOWN
----------------------------------------
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



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>