Encodings
| UTF-8 by default | |
| Magic comments | |
| Convert with encode | |
| Scrub bad bytes |
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
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
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
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.
Pass your own replacement string when the default mark confuses readers.
raw = File.read("notes.txt")
safe = raw.scrub
puts safe
Next: Time and date
Article author: Arthur Isaev