From: "Michael W. Ryder" <_mwryder@...> Date: 2007-04-24T02:40:10+09:00 Subject: Re: Question regarding design of the String Class Roland Crosby wrote: > On Apr 22, 2007, at 10:35 PM, Michael W. Ryder wrote: >> Roland Crosby wrote: >>> On Apr 22, 2007, at 10:00 PM, Michael W. Ryder wrote: >>>> Was there a reason the string class was implemented with str[i] >>>> returning the code of position i in str? The reason I ask this is >>>> that in other languages str[i] returns the string starting at >>>> position i. For example C uses t = strcpy(str[i]) and Business Basic >>>> uses S$=T$(I) to copy a string from position i. >>>> I can see no way to do this in Ruby other than using something like: >>>> t = str[i,9999]. It seemed strange that copying ranges of strings >>>> uses the same format as C (t =strncpy(str[i],n)) but not when >>>> copying the remainder. >>> Try str[i,-1], or one of the myriad other ways to access ranges of a >>> string as defined in String#[] >> If I enter: >> a = "This is a test." >> b = a[1, -1] >> puts b >> irb returns nil. Obviously this is not what I want. If instead of -1 >> I use 9999 it returns "his a test." which is what I was looking for. >> This seems like a kludge and an inconsistency. Like I pointed out >> other languages just use b = a[1] to get the remainder of the string >> instead of 104. The string class already has methods like each_byte >> for converting characters in a string to a number, so why does it need >> another shortcut for something that is probably very rarely used. > > Sorry, I meant a[1..-1] rather than a[1,-1]. I figured that out right after I posted my reply. I had forgotten about ranges as I have never programmed in a language that used them before. The little differences can really get you, especially when you find so many similarities. I don't know why Ruby > returns the character codes like that, but for what it's worth, I > believe Ruby 1.9 is going to switch to returning a single-character > string when you put one integer in String#[]. >