从1.9.3开始,如何在Ruby中获取YAML以将ASCII-8Bit字符串转换为字符串?

时间:2023-01-05 21:03:07

Here's the problem: I might have strings that are UTF-8, and I might have strings that are US-ASCII. Regardless of the encoding, I'd like YAML.dump(str) to actually dump String objects, instead of these useless !binary objects as the example shows.

这是问题所在:我可能有UTF-8字符串,我可能有US-ASCII字符串。无论编码如何,我都希望YAML.dump(str)实际转储String对象,而不是这些无用的二进制对象,如示例所示。

Is there a flag or something I'm not seeing to force YAML.dump() to do the right thing?

是否有旗帜或其他东西我没有看到迫使YAML.dump()做正确的事情?

Ruby 1.9.1 example

Ruby 1.9.1示例

YAML::VERSION              # "0.60"
a = "foo"                  # => "foo"
a.force_encoding("BINARY") # => "foo"
YAML.dump(a)               # => "--- foo\n" 

Ruby 1.9.3 example

Ruby 1.9.3示例

YAML::VERSION              # "1.2.2"
a = "foo"                  #  => "foo" 
a.force_encoding("BINARY") #  => "foo" 
YAML.dump(a)               # => "--- !binary |-\n  Zm9v\n"

Update: Got my own answer

更新:得到了我自己的答案

YAML::ENGINE.yamler='syck'
YAML.dump(a)               # => "--- foo\n" 

So, looks like using the old yamler engine with force the old behavior.

因此,看起来像使用旧的yamler引擎强制旧的行为。

1 个解决方案

#1


4  

Update: Got my own answer

更新:得到了我自己的答案

YAML::ENGINE.yamler='syck'
YAML.dump(a)               # => "--- foo\n" 

#1


4  

Update: Got my own answer

更新:得到了我自己的答案

YAML::ENGINE.yamler='syck'
YAML.dump(a)               # => "--- foo\n"