From: "David A. Black" Date: 2008-01-01T05:43:14+09:00 Subject: Re: RCR allow indexing last n items 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. 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!