Hi --

On Mon, 31 Dec 2007, Michal Suchanek wrote:

> On 30/12/2007, David A. Black <dblack / rubypal.com> wrote:
>> Hi --
>>
>> On Sun, 30 Dec 2007, Michal Suchanek wrote:
>>
>>> Hello
>>>
>>> As  discussed in thread starting with ruby-talk # 284001 there is an
>>> inconsistency in handling positive and negative range indexes to
>>> arrays and strings.
>>>
>>> Currently the requirement is that the first number in the range does
>>> fall into the string.
>>>
>>> This allows "asd'[0...10] to return the first (at most) 10 characters
>>> of the string.
>>>
>>> However, the same cannot be done on the end:
>>>
>>> "asd"[-10..-1] returns nil even though this could mean the last (at
>>> most) 10 characters as direct opposite of the above. However, the
>>> first number (-10) yields an index length-10 = -7 which is outside of
>>> the string.
>>>
>>> To make it possible to index the end of the string as easily as the
>>> beginning the rules for indexing should be changed slightly.
>>
>> I'd rather not have the index magically converted. I don't think I can
>> come up with an airtight technical reason. It just seems a bit
>> over-engineered; I'd prefer for -10 to mean -10 from the right.
>
> I want -10 to mean 10 from the right as it does now. I just want the
> bounds checking to be the same  when counting from the right as it is
> when counting from the left.

I think it's a matter of defining what it means to start taking a
substring at a non-existent point. As it now stands, you can never do
that, whether the way you reach that non-existent point is with a
positive or negative index:

>> str = "abc"
=> "abc"
>> str[4..10]
=> nil
>> str[-4..-1]
=> nil

Starting at a non-existent point is not the same as *stopping* at that
point. In other words, this is meaningful:

>> str[0..10]
=> "abc"

because you're starting in the string, at a defined point, and going
as far as you can. The problem with str[-4..-1] is not how far it
goes, but the fact that it doesn't start in the string. At least,
that's what makes it feel to me like it's not just the mirror case of
the positive one.


David

-- 
Training for 2008!
Ruby on Rails training by David A. Black/Ruby Power and Light, LLC:
    * Intro to Rails, New York, NY, February 4-7 2008
    * Advancing With Rails, New York, NY, February 11-14 2008
Hosted by Exceed Education. See http://www.rubypal.com for details!