当在红宝石中用不间断的空间充气和放气字符串时,我遇到了一个奇怪的问题。
具有常规空格的字符串表现如预期:
str = "hello world"; str_zipped = Zlib.deflate str; str == Zlib.inflate(str_zipped)
=> true
然而,
str = "hello\xA0world"; str_zipped = Zlib.deflate str; str == Zlib.inflate(str_zipped)
=> false
这是预期的行为还是错误?
答案 0 :(得分:3)
ZLib没有保留编码。您的字符串可能是UTF-8编码:
str = "hello\xA0world"
str.encoding
#=> <Encoding:UTF-8>
但是ZLib返回一个ACSII编码的字符串:
str_zipped = Zlib.deflate str
str = Zlib.inflate(str_zipped)
str.encoding
#=> <Encoding:ASCII-8BIT>
但是当你修复那个编码时:
str = "hello\xA0world"
str_zipped = Zlib.deflate str
str_utf8 = Zlib.inflate(str_zipped).force_encoding('UTF-8')
str == str_utf8
#=> true