简体   繁体   中英

ruby to_yaml colon in keys

I'm converting a hash to yaml in ruby but it adds : to keys: For example:

: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

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 .

Otherwise YAML is just trying to serialize and de-serialize as exactly as it can. A restored file with :name: x has { :name => 'x' } as the resulting data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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