Bug #4245: Array index with Range inconsistency
http://redmine.ruby-lang.org/issues/show/4245

Author: Kristian PD
Status: Open, Priority: Normal
Category: core
ruby -v: 1.8.7

I've come across what I believe to be a possible inconsistency in the implementation of the Range array index method. The following example describes my case:

array = [1,2,3,4,5]

tests
a) array[0..5] // [1,2,3,4,5]
b) array[4..-1] // [5]
c) array[5..-1] // []
d) array[6..-1] // nil

additional tests
e) array[5..10] // []
f) array[6..10] // nil

I would have expected cases c and d to both return nil according to the docs: "Returns nil if the index (or starting index) are out of range."

Out of curiosity I also added tests e and f to make sure it was not related to the end of the range being -1. 

Without having had a chance to check the code, my instinct here is that a comparison is possibly being done on the .count/.length of the array when building the result and comparing that to the first part of the range sequence.  

Am I missing another reason for this?

Kristian


----------------------------------------
http://redmine.ruby-lang.org