ruby to_yaml colon in keys

Viewed 2064

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

1 Answers

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.

Related