Kyle Decot Kyle Decot - 3 months ago 12
Ruby Question

Encoding Defaulting to ASCII-8BIT

I have the following file:

# encoding: UTF-8
a = 1 << 7
puts a.chr.encoding


This gives me back
ASCII-8BIT
. Why doesn't this give back
UTF-8
and I would expect?

Answer

chr has a encoding parameter, defaulting to ASCII, when char is < 0xff (there UTF is identical to ASCII):

128.chr.encoding                  # => #<Encoding:ASCII-8BIT>
255.chr.encoding                  # => #<Encoding:ASCII-8BIT>
256.chr.encoding                  # => RangeError: 256 out of char range
Encoding.default_internal = Encoding::UTF_8
255.chr.encoding                  # => #<Encoding:ASCII-8BIT>
256.chr.encoding                  # => #<Encoding:UTF-8>

128.chr(Encoding::UTF_8).encoding # => #<Encoding:UTF-8>
500.chr(Encoding::UTF_8).encoding # => #<Encoding:UTF-8>
Comments