From: Rob Biedenharn Date: 2013-04-18T05:33:09+09:00 Subject: Re: String index produces unanticipated result On 2013-Apr-17, at 16:24 , Thomas Luedeke wrote: > I have the following string I am pulling from a file (asterisks used to > show boundaries of line, and are not actually there: > > * 9 3 3 0 0* > > I want to test to see if a "9" is in the first five columns of the line. > Typically it is in column five, but not necessarily. > > I tried the "puts line.strip[0]" command, which returns "57". Huh? > Where did "57" come from?? Same thing happens with strip!. > > Why isn't this simply removing the leading white space and returning > "9"?? I don't understand how to accomplish my task now. > > I tried the online irb, and it gave me "9", like I'd have expected. Your local ruby is probably <1.9 and String#[] returns the integer value of the referenced character. irb1.8.7> 57.chr #1.8.7 => "9" The online version is probably >=1.9 and String#[] returns a one-character string. irb2.0.0> " 9 3 3 0 0".strip[0] #2.0.0 => "9" -Rob