Encodings
UTF-8 by default
Ruby source and strings default to UTF-8, so most languages work with no setup. Every string knows its encoding and you can ask for it.
Problems appear only at the borders: files, network, and old tools that speak other encodings. Language background lives on the
Ruby overview.
s = "Hello" puts s.encoding
ruby enc_demo.rb
UTF-8
Magic comments
A magic comment on the first line sets the source encoding for that file. It matters on old setups and for files saved in legacy encodings.
Modern editors save UTF-8 already, so you can often skip this line entirely.
# encoding: utf-8 puts "café".encoding
ruby magic_demo.rb
UTF-8
Convert with encode
encode returns the same text in a new encoding. Name the target encoding as a string and Ruby transcodes every character.
Conversions fail loudly on characters the target cannot hold, unless you pick a fallback strategy.
s = "café" wide = s.encode("UTF-16") puts wide.encoding
ruby encode_demo.rb
UTF-16
Scrub bad bytes
Bytes from broken files may be illegal in UTF-8 and crash later code. scrub replaces every bad byte with a placeholder and returns clean text.
Write one such file first, then read it back and scrub it. Pass your own replacement string when the default mark confuses readers.
File.binwrite("notes.txt", "caf\xE9") raw = File.read("notes.txt") safe = raw.scrub("?") puts safe
ruby scrub_demo.rb
caf?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What encoding does a string literal have in a modern Ruby source file?
Answer
UTF-8
Ruby source and strings default to UTF-8, so most text works with no setup.
-
What does this program print?
p "Hello".encodingAnswer
#<Encoding:UTF-8>
p prints the Encoding object in its angle-bracket form.
-
What does this program print?
s = "café" wide = s.encode("UTF-16") puts wide.encodingAnswer
UTF-16
encode returns the same text in the target encoding.
-
What does this program print?
File.binwrite("notes.txt", "caf\xE9") raw = File.read("notes.txt") safe = raw.scrub("?") puts safeAnswer
caf?
scrub replaces the invalid byte with the given replacement, so the output is plain ASCII.
-
What does this program print?
p "café".bytesizeAnswer
5
Four characters and five bytes: é takes two bytes in UTF-8.
-
What does a magic comment on the first line of a file set?
Answer
The source encoding of that file
It matters on old setups and for files saved in a legacy encoding.
-
Where do encoding problems usually appear?
Answer
At the borders: files, network, and old tools that speak other encodings
The language itself defaults to UTF-8; the data crossing a border is what surprises a program.
-
What does this program print?
p "café".encoding.nameAnswer
"UTF-8"
name answers the encoding as a string, and p prints it with quotes.
-
What does this program print?
p "caf\xE9".valid_encoding?Answer
false
The single byte E9 is not valid UTF-8, and valid_encoding? answers false without raising.
-
What does
encodedo with a character the target encoding cannot hold?Answer
Raises, unless a fallback strategy is given
A conversion fails loudly rather than corrupting the text.
Article author: Arthur Isaev