From: MonkeeSage Date: 2007-12-20T19:30:00+09:00 Subject: Re: String#[] behaviour On Dec 19, 9:01 am, Pasha Nigerish wrote: > Jordan Callicoat wrote: > > Taking your example, "'asd'[-10..-1]", this means > > 'asd'[-7..2] when you de-sugar it. Now in the other case, > > "'asd'[0...10]", once you reach #length-1, you can stop and return > > 0..#length-1. But with 'asd'[-7..2], what are you supposed to do when > > the start index is less than the first index (0)? Well, you could skip > > ahead to the first index, sure, but it makes just as much sense (if > > not more) to return nil/empty string. Same goes for cases such as > > 'asd'[-2..-3] (i.e., 'asd'[1..0]), where the start index is greater > > than the end index. > > IMHO, the main goal of such a construct (some_string[-10..-1]) - to > return last 10 chars from some_string. And in this case - returning > 'asd' for 'asd'[-10..-1] seems to be as logical as returning 'asd' for > 'asd' for [0..10] (as implemented now). > > right now (1.8.6) we have: > 1) 'asd'[0..10] => 'asd' > 2) 'asd'[2..1] => '' > 3) 'asd'[-1..-2] => '' > -BUT- > 4) 'asd'[-10..-1] => nil > > I think, that by a "Principle of Least Astonishment" (c) we can unify > that cases - i.e. to return either 'asd' or nil in cases 1) and 4). All > that we need - adjust start index of the range to 0, if negative - right > after de-sugar. > -- > Posted viahttp://www.ruby-forum.com/. In thinking about it, I guess that does make some sense. Unless one were to assume that negative starting indexes were more likely to be programmer errors than larger-than-#length-1 end indexes (does anyone claim this?), it seems to me that setting negative indexes to 0 would be consistent with setting larger-than-#length-1 indexes to #length-1. Maybe you should start an RCR for this. Regards, Jordan