3 ms·
> Changing the encoding of a string effectively destroys the data inside. You are assuming the string had the right pair of <encoding type, byte value> which i
by riffraff 14y ago
> Changing the encoding of a string effectively destroys the data inside.
You are assuming the string had the right pair of <encoding type, byte value> which is not guaranteed.
For example, an old mysql binding wouldn't handle some of the encoding settings correctly, so you'd get a UTF-8 string with attached data in latin-something.
This could be trivially fixed in client code by simply calling #force_encoding.
- lucian1900 14y agoThat is a bug that is more easily (and less dangerously) fixed by asking for bytes and then decoding them from latin-1.
- stouset 14y agoAnd yet, in 99.5% of use-cases, it's infinitely more convenient not to have to jump through that hoop. I've been a Ruby developer for… seven years now, and have literally never encountered actual problems in the real world by this, by mutable hash keys, or well over half the other issues on this list. And I'm willing to bet the vast majority of other developers haven't either. Ruby has mostly sensible defaults that work as expected in most practical cases.