Ruby 19 Encoding
Ruby 1.9 was released last week, and I’ve been poking around with the changes to the String class to see what’s possible. 1.9 is a development release, so I’m not planning to start using it in production anytime soon, but it’s nice to be able to test my apps for compatibility with the planned 2.0 release.
In ruby <= 1.8, strings were effectively just byte streams. Those bytes would often contain text in one encoding or another, but there was no formal way to record exactly which one (if any; binary data has none). All the default methods assumed single byte encoding (such as US-ASCII), so behaviour was odd when the encoding was multibyte (such as UTF8).
1.9 now records the encoding of a string, which allows many of the methods to be improved to recognise multi-byte strings. For further background on encodings, read Joel Spolsky’s article.
Consider the following UTF-8 string:
The size method now returns the size of the string in characters, not bytes. To get the size in bytes, use the bytesize method.
To check if the string contains only ASCII compatible characters, regardless of the encoding:
If Ruby has confused itself and has the wrong encoding recorded, you can forcibly fix it. The bytestream will not be modified, just ruby’s understanding of what the stream represents.
Some encodings can be translated to others: the underlying bytestream will be changed to represent the same characters in the new encoding. The output of the following examples won’t render correctly as your browser will be interpreting this page as UTF-8:
If you try to encode a string into an encoding for which there are no equivalent characters (ie. a UTF-8 string with Hebrew characters into Shift-JIS), then a runtime exception will be raised.
To view the encodings available on the system:
To set the default encoding to use in your scripts, use the following magic comment on the first line (or second if the first starts with #!):
If you are reading in a file that is encoded differently to your default, you can specify what to use when opening it:
All this encoding sugar makes for a promising future for Ruby’s international support. I see it being useful in a few of the libraries I maintain to ensure developers pass in strings with the correct encoding.
For a more detailed look at the encoding support in 1.9, I highly recommend to chapter on encoding in the 3rd Edition of the pickaxe.