June 26, 2025
Have you ever wondered how Ruby interprets characters behind the scenes? During some recent coding exploration, I dove deep into .chr and .ord—two Ruby methods that reveal how characters are represented at the byte and code point level.
Integer#chr and String#ord: What Are They?
Ruby allows you to convert between characters and their numeric representations easily:
65.chr # => "A"
120.chr # => "x"
"A".ord # => 65
"x".ord # => 120
These conversions are based on the ASCII table—a foundational character encoding standard that maps numbers to characters.
What About Accented Characters?
Things get more interesting when working with non-ASCII characters like “á”:
"á".ord # => 225
225.chr # => "\xE1" # Wait... what?
Why doesn’t it return “á”?
Because .chr without an encoding assumes ASCII-8BIT (binary) and just returns the raw byte \xE1. But UTF-8 handles “á” as a multi-byte character.
225.chr(Encoding::UTF_8) # => "á"
Now Ruby knows to interpret it using the right encoding.
What About Emojis?
" ".ord # => 128512
128512.chr(Encoding::UTF_8) # => " "
UTF-8 allows Ruby to handle a massive range of Unicode characters, including emojis, accented letters, and non-Latin scripts. Under the hood, emojis are stored as multi-byte sequences:
" ".bytes # => [240, 159, 152, 128]
" ".encoding # => #<Encoding:UTF-8>
Takeaways
- ASCII covers basic Latin characters (0–127)
- Latin-1 extends to characters like “á” (128–255)
- UTF-8 supports everything from “á” to “
”
- Use .chr(Encoding::UTF_8) for full Unicode support
Whether you’re dealing with classic ASCII or modern emoji-rich strings, understanding these methods gives you powerful insight into how Ruby manages character data.
Have you used .chr or .ord in creative ways? Let me know—I’d love to see more use cases!
Top comments (0)