I am writing an method that takes variable lengths of arrays and compresses and converts them to matching array lengths.
The method accepts an array length of anywhere from 50 to 126 . .. and converts them down based on parameters when called. This means is I am dynamically accessing ranges that are specified at method call. This also means I will be accessing indices of the array that won't exist. In addition to this, the range I am accessing may be equal. This is where my problem lies: it appears that the terminating pocket of an array behaves strangely when accessing it with a range, as opposed to any other non-existent index
>> a = [0, 1, 2, 3, 4]
# => [0, 1, 2, 3, 4]
>> a[5]
# => nil
>> a[4..4]
# => [4]
>> a[6..6]
# => nil
>> a[5..5]
# => [] <------GGRAAAAAhhh!!! Thought you were nil
>> a[5..7]
# => [] <-------you too
>> a[6..7]
# => nil <-----but you are behaving
Does anyone know why this happens? Its an extra test against an empty array for my code, but I am more curious after chasing this bug down in my rails app all day. FWIW I am running Ruby 1.8.6 on Mac OS X, patchlevel 114