------------------------------------------------------------------------
Returns a string containing the ASCII character represented by the
receiver's value.
65.chr #=> "A"
?a.chr #=> "a"
230.chr #=> "\346"
There is no ascii character with an ascii code equal to 8364. The pound
has been around a long time, and it made it into extended ascii (or
latin-1, which uses 8 bits). The euro is a recent invention, and it's
numerical code is way out in unicode land.
arr =
arr << dec_num
str = arr.pack("U") #U=UTF-8 => encode unicode 8364 into a UTF-8
character.
puts str
$ri chr
------------------------------------------------------------ Integer#chr
int.chr => string
------------------------------------------------------------------------
Returns a string containing the ASCII character represented by the
receiver's value.
65.chr #=> "A"
?a.chr #=> "a"
230.chr #=> "\346"
There is no ascii character with an ascii code equal to 8364. The pound
has been around a long time, and it made it into extended ascii (or
latin-1, which uses 8 bits).
8 bits can be used to store codes between 0-255.
ascii characters are represented by numerical codes between 0-127, so
the text describing the operation of chr in the docs is wrong.
According to the description, you would expect codes above 127 to
produce errors. But codes between 127-255 do not produce errors. The
last example demonstrates that. The docs should read something like:
Returns a string containing the latin-1 (or ISO-8859-1) character
represented by the receiver's value. Valid character codes are 0-255.
$ri chr
------------------------------------------------------------ Integer#chr
int.chr => string
------------------------------------------------------------------------
Returns a string containing the ASCII character represented by the
receiver's value.
65.chr #=> "A"
?a.chr #=> "a"
230.chr #=> "\346"
There is no ascii character with an ascii code equal to 8364. The pound
has been around a long time, and it made it into extended ascii (or
latin-1, which uses 8 bits).
8 bits can be used to store codes between 0-255.
ascii characters are represented by numerical codes between 0-127, so
the text describing the operation of chr in the docs is wrong.
According to the description, you would expect codes above 127 to
produce errors. But codes between 127-255 do not produce errors. The
last example demonstrates that. The docs should read something like:
Returns a string containing the latin-1 (or ISO-8859-1) character
represented by the receiver's value. Valid character codes are 0-255.
Thanks, good stuff. I didn't know about Array#pack
Sure, I'm familiar with character sets, I was assuming the 'same as
Perl' chr functionality, I should have checked the rdoc for chr,...
Perl:
chr Returns the character represented by that NUMBER in the
character set. For example, "chr(65)" is "A" in either
ASCII
or Unicode, and chr(0x263a) is a Unicode smiley face.
Ruby:
int.chr => string
···
------------------------------------------------------------------------
Returns a string containing the ASCII character represented by the
receiver's value.
$ri chr
------------------------------------------------------------ Integer#chr
int.chr => string
------------------------------------------------------------------------
Returns a string containing the ASCII character represented by the
receiver's value.
65.chr #=> "A"
?a.chr #=> "a"
230.chr #=> "\346"
There is no ascii character with an ascii code equal to 8364. The pound has been around a long time, and it made it into extended ascii (or latin-1, which uses 8 bits).
8 bits can be used to store codes between 0-255.
ascii characters are represented by numerical codes between 0-127, so the text describing the operation of chr in the docs is wrong. According to the description, you would expect codes above 127 to produce errors. But codes between 127-255 do not produce errors. The last example demonstrates that. The docs should read something like:
Returns a string containing the latin-1 (or ISO-8859-1) character represented by the receiver's value. Valid character codes are 0-255.
Thanks, good stuff. I didn't know about Array#pack
Sure, I'm familiar with character sets, I was assuming the 'same as Perl' chr functionality, I should have checked the rdoc for chr,...
Perl:
chr Returns the character represented by that NUMBER in the
character set. For example, "chr(65)" is "A" in either ASCII
or Unicode, and chr(0x263a) is a Unicode smiley face.
Ruby:
int.chr => string
------------------------------------------------------------------------
Returns a string containing the ASCII character represented by the
receiver's value.