--00163683302a850ac9048e7bb096 Content-Type: text/plain; charset=ISO-8859-1 2010/8/23 Yukihiro Matsumoto <matz / ruby-lang.org> > ...In message "Re: assert "foo"[3] ! foo"[3,1]...revisited" > on Mon, 23 Aug 2010 17:51:02 +0900, timr <timrandg / gmail.com> writes: > > |I don't like that this passes > |assert "foo"[3] ! foo"[3,1] > |... > > "foo"[3,1] is "" since when index is within the string, the sought > length will be rounded to fit in the size. And 3 (which equals to the > length of the string) is considered as touching the end of the string, > so the result length is zero. > ... > matz. > I'm grateful for the reply, and for the question that prompted it, as that is something that has bothered/intrigued me for some time, and I had wondered whether it was something to do with string (or array) processing near the end of the string. I think it's worth adding something like Matz's wording to http://www.ruby-doc.org/core/classes/String.html#M000771 http://www.ruby-doc.org/core/classes/Array.html#M002160 because reasons for why things work a particular way do help people (well, at least me) remember behaviour. I first noticed this when I was doing some complicated string processing, and was using (or trying to use) something like str[i, 1] being nil to indicate that the end of the string had been reached. (I could perhaps have used regular expressions, but I didn't - and sometimes still don't - trust my understanding of them, and I was very wary of an apparent regular expression match or non-match being not quite what I'd intended it to be.) I then found that arrays worked the same way. At the time I wondered about asking whether it might be of more general use if String and Array also had a variation of slice having the behaviour I wanted for what I was doing (that is working like slice, except when the index is just past the end of the string or array when it would return nil), but didn't pursue it, partly from assuming that since [] / slice were fairly fundamental parts of String and Array it was very likely this had been debated before, even if I couldn't find the debate. I'm tempted to suggest that now, partly because I would find it useful (not sure what to call it, but it should start with "slice") - yes, I know I could write something to do that for my own use! - and partly because if you (well I) have two similar but a little differently named methods it might help you (well me) remember that there is a behaviour difference to what - choosing my words very carefully - I am expecting. (And being well aware that *I* didn't invent the Ruby computer language.) *** off the topic of the thread, but on the topic alluded to by my last comments, and prompted by Matz's penultimate sentence: I'm rather distrustful of slogans - I think there is a danger that they start being used as a substitute for really thinking about things, so I was pleased when the use of a certain phrase in Ruby discussions became discouraged (quite a long time ago now). I don't use Python, but I do sometimes look at the discussion groups, and I get the impression that "there should only be one obvious way to do it" sometimes (frequently???) gets misused, not least by the omission of "obvious". It also illustrates nicely how slogans can get corrupted: looking here http://www.python.org/dev/peps/pep-0020 I find that (a) the original (?) version is more complex, and (b) more than somewhat self-deprecating, which I like, because it at least hints at the possibility that different people can quite reasonably take different views on things. There should be one -- and preferably only one -- obvious way to do it. Although that way may not be obvious at first unless you're Dutch. --00163683302a850ac9048e7bb096--