From: Michal Suchanek Date: 2007-12-19T21:00:38+09:00 Subject: Re: String#[] behaviour On 19/12/2007, Pasha Nigerish wrote: > Michal Suchanek wrote: > > On 18/12/2007, DNNX <6aLLIaPuMoB@gmail.com> wrote: > >> > > The asymmetry is in that you can chop off "at most 10 characters from > > the start" with 0...10 but not "at most 10 characters from the end" > > with -10..-1 because the start that has to be inside the string is the > > one of which you cannot be sure. You cannot swap the bounds because > > you get an empty string then. > > > > So the symmetric rule for range indexing would be something like this: > > ...skipped... > > So we must become clear with range indexing: I think it's perfectly > legal to return intersection of an array/string with range instead of > nil in a case of negative start. > This can be done via one-line patch in range.c:615 (as in trunk) - just > assume `beg = 0` instead of `goto out_of_range` > Thus we'll have at least more perl-compatible behavior =) i.e. just as > 'abc'[0..6] is 'abc' now, so 'abc'[-6..-1] will be 'abc' as well. > > One problem I see in this assumption: 'abc'[4..6] and 'abc'[-6..-4] will > return '' instead of nil. You can still test the lower bound is inside the string. It's just that with negative ranges the lower bound is the second number, not the first. Thanks Michal