From: Steven Lumos Date: 2006-09-27T07:51:04+09:00 Subject: Re: string#[] Newbie writes: > Thomas Adam wrote: >>> This must be a common newbie question, but I can't find the answer. >>> >>> Why does string#[] return an ASCII code, rather than a character? >>> >>> "abc"[1,2] #-> "bc" >>> "abc"[1..2] #-> "bc" >>> "abc"[1] #-> 98 >> It tells you why in "ri String#[]": >> `` >> Element Reference---If passed a single +Fixnum+, returns the code >> of the character at that position. If passed two +Fixnum+ objects, >> returns a substring starting at the offset given by the first, and >> a length given by the second. If given a range, a substring >> containing characters at offsets given by the range is returned. >> '' >> -- Thomas Adam > That answers what?, which I already knew. I'm asking why? Probably because a one-character String is 22 bytes and a Fixnum is 4 (on 32-bit archs). As James said, a future version of Ruby will return a one-character string in that case. In the meantime if you are just doing comparisons you can use the Ruby character literal syntax: next if line[0] == ?# Steve