Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 具有不间断空间的Zlib_Ruby_Whitespace_Zlib - Fatal编程技术网

Ruby 具有不间断空间的Zlib

Ruby 具有不间断空间的Zlib,ruby,whitespace,zlib,Ruby,Whitespace,Zlib,在ruby中,当使用不间断空格对字符串进行充气和放气时,我遇到了一个奇怪的问题 带有规则空格的字符串的行为符合预期: 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) =>

在ruby中,当使用不间断空格对字符串进行充气和放气时,我遇到了一个奇怪的问题

带有规则空格的字符串的行为符合预期:

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

这是预期的行为还是错误?

ZLib没有保留编码。您的字符串可能是UTF-8编码的:

str = "hello\xA0world"
str.encoding
#=> <Encoding:UTF-8>
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