Add an option for stringifying the symbol key without colon
Currently if the key in the Hash is a symbol (e.g. h = {a: 1}), to_yaml will generate :a: 1.
irb(main):003:0> require 'yaml'
irb(main):003:0> puts({a: 1, b: 'ruby'}.to_yaml)
---
:a: 1
:b: ruby
Usually the key in the yaml file doesn't start with ':'. So this feature request is for adding a new to_yaml option for stringifying the symbol key without colon. As a result, to_yaml will generate 'a: 1' for '{a: 1}'.
The code change required will be in this function: https://github.com/ruby/psych/blob/e01839af57df559b26f74e906062be6c692c89c8/lib/psych/visitors/yaml_tree.rb#L401 Change it to:
def visit_Symbol o
if o.empty?
@emitter.scalar "", nil, '!ruby/symbol', false, false, Nodes::Scalar::ANY
else
str = @options[:stringify_symbol_key] ? "#{o}" : ":#{o}"
@emitter.scalar str, nil, nil, true, false, Nodes::Scalar::ANY
end
end
There are a few discussion/requirement for this feature on google.com, e.g. https://stackoverflow.com/questions/53093316/ruby-to-yaml-colon-in-keys
I'd really like to have this feature. I'm trying to write a DSL for Gitlab CI and because of this behaviour I have to resort to a bunch of nasty hacks to convert all symbols in all hashes, collections and other objects into strings.
I would like to also have this option. It is very uncommon to have symbols in YAML files. Most of them are normal strings and not symbols.