From: Jonas Pfenniger Date: 2008-01-01T22:59:54+09:00 Subject: Re: RCR allow indexing last n items 2008/1/1, David A. Black : > Hi -- > > On Tue, 1 Jan 2008, Michal Suchanek wrote: > > > On 31/12/2007, David A. Black wrote: > >> Hi -- > >> > >> On Mon, 31 Dec 2007, Michal Suchanek wrote: > >> > >>> On 31/12/2007, David A. Black wrote: > >>>> 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. > >>> > >>> It's just a matter of point of view. You advocate the point of the > >>> string implementation which always counts from the start of the string > >>> but I am advocating the view from the interface side that pretends to > >>> count from the end. And that's what the people using ruby see. > >> > >> I don't mean the start of the string; I mean the starting index, into > >> the string, of the first character you want. If it's -10 on a > >> 3-character string, then it's an index that's out of range. > >> > >> I guess you could look at it by saying there's no starting point; > >> there's just n characters, with leading or trailing non-existent ones > >> chopped off. I'm just not sure I like the semantics of having str[-10] > >> be nil, but str[-10..anything] being a string. > >> > > no, it's not -10..anything, it's -10..anything as long as it defines a > > part of the string. > > What I mean is: if str[-10] is nil, then I'm not sure about there > being any x, such that str[-10..x] is a string. > > I understand your reasoning, in terms of intervals and intersections. > It's definitely a less "magic" way of looking at it. And there is yet another case : "abc"[0,0] #=> "" if "abc"[0,0] would return nil, then you could coerce the range so that [-10, -5] would give [0,0], which would return nil. -- Cheers, zimbatm