Brian Candler wrote:
> -------- main.rb (your program) --------
> # encoding: ASCII-8BIT
> 
> require 'library'
> binary_data = "\xff\xee\xdd"
> msg = Library.err_to_str
> binary_data << [msg.bytesize].pack("N")
> binary_data << msg
> 
> -------- library.rb (someone else's code that you don't control) 
> --------
> # encoding: UTF-8
> 
> module Library
>   def self.err_to_str
>     "über-error"
>   end
> end
> 
> $ ruby19 main.rb
> main.rb:7:in `<main>': incompatible character encodings: ASCII-8BIT and 
> UTF-8 (Encoding::CompatibilityError)

I should add: if ruby 1.9 *always* gave an exception when an ASCII-8BIT 
string encountered a UTF-8 String, it wouldn't be a problem: your unit 
tests would pick up the failure quickly.

But maybe in this library you're using, 99% of the error message don't 
have any extended characters (i.e. those with the top bit set). Those 
will work fine, even if tagged as UTF-8. It's only on the occasion where 
the library decides to return a string which is tagged UTF-8 *and* 
contains extended characters that the runtime crash will occur - and 
this means you're always wondering whether you have sufficient coverage.

As a workaround, you might have to add extra unit tests which stub out 
the library and force it to return a message with high-bit characters in 
it, and check that your program behaves as expected. But mocking every 
single library API which might return a string is really painful.
-- 
Posted via http://www.ruby-forum.com/.