Possible Duplicate:
Array slicing in Ruby: looking for explanation for illogical behaviour (taken from Rubykoans.com)
I’m following Ruby Koans and I’ve gotten to a part that deals with an array that looks like this:
array = [:peanut, :butter, :and, :jelly]
One of the tests focuses on what array[4,0] returns, and another focuses on what array[5,0] returns.
There are only 4 elements in this array, meaning it goes up to array[3], correct? So why is array[4,0] returning a blank array while array[5,0] returns nil?
The [i, n] form is identifying substring boundaries and not characters
The short answer is that you are defining a substring to either return or replace.
There is a zero-length string at the beginning and at the end that needs to be identifiable.
In the two-argument index, the positions are really the individual boundaries between the characters, and there is one such boundary after the last character.