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.

  1. 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.

  2. What does this program print? p "Hello".encoding

    Answer

    #<Encoding:UTF-8>

    p prints the Encoding object in its angle-bracket form.

  3. What does this program print? s = "café" wide = s.encode("UTF-16") puts wide.encoding

    Answer

    UTF-16

    encode returns the same text in the target encoding.

  4. What does this program print? File.binwrite("notes.txt", "caf\xE9") raw = File.read("notes.txt") safe = raw.scrub("?") puts safe

    Answer

    caf?

    scrub replaces the invalid byte with the given replacement, so the output is plain ASCII.

  5. What does this program print? p "café".bytesize

    Answer

    5

    Four characters and five bytes: é takes two bytes in UTF-8.

  6. 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.

  7. 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.

  8. What does this program print? p "café".encoding.name

    Answer

    "UTF-8"

    name answers the encoding as a string, and p prints it with quotes.

  9. 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.

  10. What does encode do 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