Binary string literals in Ruby 2.0

2019-03-13 16:21发布

问题:

When upgrading to Ruby 2.0, a test case started to fail:

expected = "\xD1\x9B\x86"
assert_equal expected, actual

with the following message:

<"ћ\x86"> expected but was
<"\xD1\x9B\x86">.

The actual variable contains a binary string obtained from an external library call.

The problem is that the default encoding of source files (and therefore string literals) changed in Ruby 2.0 from US-ASCII to UTF-8.

回答1:

The solution is to change the definition of the string literal to enforce its encoding. There are a few possible options to do 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")


标签: ruby encoding