Letters, bytes and emoji
So far a character has been a letter from the keyboard. But a Ruby string can hold every script in the world: Swiss German, Japanese, emoji. And all at once a simple question gets tricky: how long is a string? length counts its characters, bytesize the bytes the computer stores for them:
%w[bacon Grüezi ルビー 🦊].each do |word|
puts "#{word}: length #{word.length}, #{word.bytesize} bytes"
endThe computer stores text as bytes, numbers from 0 to 255. How a character turns into bytes is decided by the encoding, and Ruby's is UTF-8: the letters from A to Z take one byte each, ü takes two, Japanese kana three and an emoji four. bytes shows them, encoding names the table:
p "bacon".bytes
p "Grüezi".bytes # ü is 195, 188
p "🦊".bytes
"Grüezi".encodingBehind the bytes, every character has a number of its own in the great Unicode table, which lists about 150 000 characters from all the world's scripts: its code point. A is 65, ü is 252, the fox is 129418. ord gives the code point of a character, codepoints those of a whole string. And with \u{...} you write a character into a string by its number – in hexadecimal, which lesson 26 explains; 1F98A is 129418:
p "A".ord
p "ü".ord
p "🦊".ord
p "Grüezi".codepoints
"\u{1F98A} loves \u{1F953}"So is a character one code point? Not always. What you see as one sign can be made of several: the Swiss flag 🇨🇭 is two code points, the letters C and H from a special flag alphabet, and a family emoji is three people glued together with invisible joiners. chars splits a string into code points, grapheme_clusters into what a person sees as one character:
flag = "🇨🇭"
p flag.length
p flag.chars
p flag.grapheme_clusters
family = "👨👩👧"
p family.length
family.grapheme_clusters.lengthA family of length 5 – the two joiners count too. The difference matters as soon as you take text apart. reverse turns the code points round, and the flag's C H becomes H C: two lonely letters, no country. Reversed by grapheme clusters, every emoji stays in one piece:
greeting = "Grüezi 🇨🇭"
p greeting.reverse
greeting.grapheme_clusters.reverse.joinupcase and downcase know more than A to Z – umlauts and accents change too. And for German they have a surprise: ß, the sharp s that the Swiss never write, has no capital in everyday use, so upcase makes SS of it – just what Swiss spelling does anyway. The word grows longer on the way:
p "ÜBER".downcase
p "smörgåsbord".upcase
p "straße".upcase
p "straße".length
"straße".upcase.lengthThe sneakiest trap of all: two strings that look exactly alike and still aren't equal. An é can be stored as one code point, or as a plain e followed by a combining accent (\u0301) that settles on the letter before it. Text from a Mac's file names or from a copied web page can come either way. unicode_normalize brings both into the same form:
one = "Café"
two = "Cafe\u0301" # e + a combining accent
puts one, two
p one == two
p [one.length, two.length]
one.unicode_normalize == two.unicode_normalizeNormally unicode_normalize puts letter and accent together (that form is called NFC). With :nfd it does the opposite and takes them apart: first the bare letter, then the accent on its own. In a regex, such accents are \p{Mn} – “mark, nonspacing”, a sign that takes no room of its own. So you can find them, count them … or remove them:
apart = "Zürich".unicode_normalize(:nfd)
p apart.chars
p apart.length
apart.scan(/\p{Mn}/).lengthA rule of thumb: length counts code points, bytesize counts storage, and only grapheme_clusters counts what a person sees. And text that comes from outside you normalize before you compare it.
length counts code points, and people count what they see. Fill sizes with three numbers for every post – its bytes, its code points and the characters a person sees; for "Grüezi!" that is [8, 7, 7]. And backwards gets every post reversed, with every emoji in one piece.posts = ["Grüezi!", "ルビー", "🦊❤️🥓", "Hopp 🇨🇭!", "👨👩👧"]
# sizes = ... # per post: [bytes, code points, characters you see]
# backwards = ... # per post: reversed, every emoji in one piece
- instead of spaces – "Crème brûlée" becomes "creme-brulee". The trick from the last cell helps: take the letters apart with :nfd, then remove every \p{Mn}.dishes = ["Crème brûlée", "Café au lait", "Éclair",
"Crêpe with bacon", "Jalapeño pizza"]
# slugs = ... # every dish without accents, in lower case, "-" for spaces