From: Benoit Daloze Date: 2010-04-18T01:48:57+09:00 Subject: Re: what is String#ord? --0016363ba6e8a6f0430484718052 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable On 17 April 2010 18:41, Xavier Noria wrote: > On Sat, Apr 17, 2010 at 5:35 PM, Xavier Noria wrote: > > > Ruby 1.9 docs for String#ord say: > > > > Return the Integer ordinal of a one-character string. > > > > What does that mean? Check for example > > > > "=D7".ord # =3D> 215 > > "=D7".bytes.to_a # =3D> [195, 151] > > Trial and error suggests it is the code of the character in the > encoding of the string: > > euro =3D "\u20AC" > > euro.ord.to_s(16) # =3D> "20ac" > euro.encode("iso-8859-15").ord.to_s(16) # =3D> "a4" > > That is what the source code suggests also: > > VALUE > rb_str_ord(VALUE s) > { > unsigned int c; > > c =3D rb_enc_codepoint(RSTRING_PTR(s), RSTRING_END(s), STR_ENC_GET(s))= ; > return UINT2NUM(c); > } > > p "=D7".ord # =3D> 215 p "=D7".bytes.to_a # =3D> [195, 151] p "=D7".encoding # =3D> # p "=D7".codepoints.to_a #=3D> [215] In UTF-8, (and Unicode in general), one byte is not always(or even never) a character. A codepoint represent a character ;) So, you can think of ord as codepoints[0], and that number of course depend= s of the String's Encoding. Regards, B.D. --0016363ba6e8a6f0430484718052--