Gray Soft / Character Encodings / Ruby 1.8 Character Encoding Flawstag:graysoftinc.com,2014-03-20:/posts/752014-04-18T16:16:14ZJames Edward Gray IIRuby 1.8 Character Encoding Flawstag:graysoftinc.com,2008-12-11:/posts/752014-04-18T16:16:14ZLet's take a quick look at what's not working Ruby 1.8's character encoding support so we can better understand where all of these Ruby 1.9 changes are coming from.<p>Now that we have toured the entire landscape of Ruby 1.8's encoding support, we need to discuss the problems the system has. These long standing issues are what pushed the core team to build the m17n (multilingualization) implementation for Ruby 1.9.</p>
<p>The main problems are:</p>
<ul>
<li>Not enough encodings supported</li>
<li>
<code>Regexp</code>-only support just isn't comprehensive enough</li>
<li>
<code>$KCODE</code> is a global setting for all encodings</li>
</ul><p>I imagine most of those are pretty straightforward, but let's talk through them just to make sure we learn from the mistakes of the past. I'm pretty sure this will make it easier to understand why things are the way they are in Ruby 1.9.</p>
<p>The "not enough encodings" complaint should be the most obvious of all. Ruby 1.8 supports four and one is just no encoding. That means you really only get UTF-8 and two Asian encodings. The UTF-8 support is how we've managed to make it this far, but there are a ton of common encodings that just aren't covered.</p>
<p>The most important thing to realize here though is that we can't just keep adding encodings to Ruby 1.8. The system wasn't designed with that in mind. We will run out of letters to tack onto the end of a <code>Regexp</code> very fast. It's just not practical.</p>
<p>Once we have more encodings, it's time to get serious about wider support. <code>Regexp</code> was probably the one place where the core team was able to reap the biggest rewards form a little encoding hack, but it really just allows us to divide up characters. There's a lot more to encodings than that. What about checking if encoded data is valid, working with character groups, or examining Unicode code points? <code>Regexp</code> alone can't solve all of those problems.</p>
<p>Finally, one giant encoding switch is dangerous. There are several places where you need to know an encoding: the data in a <code>String</code>, the data being read from an <code>IO</code> object, and the encoding your source itself is written in, for example. In Ruby 1.8, you can't differentiate those things. You can just set it in one place. What if I write my source in UTF-8 and set <code>$KCODE</code> accordingly, but then load your library that's written in Shift JIS? One of us isn't going to get our way and that can't be good for the code.</p>
<p>Again, I just wanted highlight these issues, because I think it will help clarify why things are changing in Ruby 1.9. As we now dig into Ruby 1.9 encodings keep an eye out for these specific flaws and how they are being addressed…</p>James Edward Gray II