From: Adam Prescott Date: 2011-03-03T19:27:57+09:00 Subject: Re: How can i get the first letter of this string --90e6ba5bbab71bdead049d917b0f Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On Thu, Mar 3, 2011 at 5:32 AM, Thiago Massa wrote: > irb(main):023:0> bacon=3D"chunky" > =3D> "chunky" > irb(main):024:0> bacon[0] > =3D> 99 > irb(main):025:0> bacon[0].chr > =3D> "c" > > 99 is the ascii number that represents the letter 'c'. > > I think that explains what you want. > On Thu, Mar 3, 2011 at 5:54 AM, Michael Edgar wrote: > That won't work on Ruby 1.9, as bacon[0] will return the first actual > character. This technique > works on both: > > >> bacon =3D "chunky" > =3D> "chunky" > >> bacon[0,1] > =3D> "c" > This will not work on Ruby 1.8, because the character involved is a two-byt= e character, and before 1.9 there's nothing but bytes. s =3D "=C3=A1" s.inspect #=3D> =3D> "\303\241" s[0] #=3D> "\303" s[0, 1] #=3D> "\303" s[0].chr #=3D> "\303" Since you're on 1.8.7, you could use $KCODE =3D "U" # UTF-8 s.chars.first #=3D> "=C3=A1" Note that if you do not set $KCODE, you will just get "\303". --90e6ba5bbab71bdead049d917b0f--