kungfu-free
3/31/2009 4:05:00 PM
ruby -e 'puts "\244"'
David Wright a écrit :
> 7stud -- wrote:
>> 7stud -- wrote:
>>> $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.
>
irb(main):019:0> str = "\244"
=> "\244"
irb(main):020:0> puts str
?
=> nil