简体   繁体   English

ruby to_yaml 2.0.0添加了!ruby / object:Hash但YAML.load不会读取它

[英]ruby to_yaml 2.0.0 adds !ruby/object:Hash but YAML.load won't read it

Is there a way to disable ruby 2.0.0 YAML's suffixing with ruby type info? 有没有办法禁用ruby 2.0.0 YAML的后缀为ruby类型的信息?

I just upgraded to ruby 2.0.0 and I'm having YAML problems. 我刚升级到ruby 2.0.0,但遇到YAML问题。 I read-update-write a yaml file that previously looked like this 我读-更新-写一个以前看起来像这样的Yaml文件

test:
  test2:
    somevar: hello

When I feed this into ruby 200, it reads OK but writes: 当我将其输入ruby 200时,它显示为OK,但写道:

test: !ruby/object:Hash
  test2: !ruby/object:Hash
    somevar: hello

When I rerun the program YAML.load reads nothing. 当我重新运行该程序时,YAML.load不会读取任何内容。

myH = YAML.load_file( YAML_FPATH )
puts "Yaml as Hash:" + myH.inspect
>> Yaml as Hash: {}

Curiously, if I pass the "test: !ruby/object:Hash" version of the file to ruby 1.8.7, it reads the suffixed file OK and writes a non-suffixed file. 奇怪的是,如果我将文件的“ test:!ruby / object:Hash”版本传递给ruby 1.8.7,它将读取带后缀的文件,然后写入一个非带后缀的文件。

Problem found: I modified hash.to_yaml so that it sorts the keys. 发现问题:我修改了hash.to_yaml,以便对键进行排序。 Since my code is based on hash.to_yaml 1.8.7 I need to revise it based on hash.to_yaml 2.0.0. 由于我的代码基于hash.to_yaml 1.8.7,因此我需要根据hash.to_yaml 2.0.0对其进行修改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM