简体   繁体   English

红宝石to_yaml键中的冒号

[英]ruby to_yaml colon in keys

I'm converting a hash to yaml in ruby but it adds : to keys: For example: 我将红宝石中的哈希值转换为yaml,但它在键中添加了:例如:

:name: "Name"
:value: "Value"

If there a way to avoid and just output: 如果有避免的方法,而只输出:

name: "Name"
value: "Value"

I can do this easily with gsub, But just curious 我可以用gsub轻松做到这一点,但是很好奇

If you use string keys you won't get those prefixes. 如果使用字符串键,则不会获得这些前缀。 That's what happens when you serialize something with symbol keys. 这就是使用符号键序列化某些内容时发生的情况。

In Rails or using ActiveSupport you can call deep_stringify_keys to convert them all prior to a YAML.dump . 在Rails或使用ActiveSupport中,您可以调用deep_stringify_keysYAML.dump之前将其全部转换。

Otherwise YAML is just trying to serialize and de-serialize as exactly as it can. 否则,YAML只会尝试尽可能完全地序列化和反序列化。 A restored file with :name: x has { :name => 'x' } as the resulting data. 使用:name: x还原的文件具有{ :name => 'x' }作为结果数据。

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

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