encoding - Binary string literals in Ruby 2.0 -


when upgrading ruby 2.0, test case started fail:

expected = "\xd1\x9b\x86" assert_equal expected, actual 

with following message:

<"ћ\x86"> expected <"\xd1\x9b\x86">. 

the actual variable contains binary string obtained external library call.

the problem default encoding of source files (and therefore string literals) changed in ruby 2.0 us-ascii utf-8.

the solution change definition of string literal enforce encoding. there few possible options this:

use array#pack (all versions of ruby):

expected = ["d19b86"].pack('h*') 

use string#b (ruby >= 2.0 only):

expected = "\xd1\x9b\x86".b 

use string#force_encoding (ruby >= 1.9 only):

expected = "\xd1\x9b\x86".force_encoding("ascii-8bit") 

Comments