3

I'm trying to generate a hash in yaml format, but by default, this method always adds --- in front of the object.

> h = {key1: 'v1', key2: 'v2'}
=> {:key1=>"v1", :key2=>"v2"}
> h.deep_stringify_keys.to_yaml
=> "---\n:key1: v1\n:key2: v2\n"
rplaurindo
  • 1,277
  • 14
  • 23

2 Answers2

3

Brute force, but it'll do the trick:

h.deep_stringify_keys.to_yaml[3..-1]
davidrac
  • 10,723
  • 3
  • 39
  • 71
1

Another way is to cut the first line:

irb:

>> h = {key1: 'v1', key2: 'v2'}
>> require 'yaml'
>> h.to_yaml
=> "---\n:key1: v1\n:key2: v2\n"
>> h.to_yaml.lines[1..-1].join
=> ":key1: v1\n:key2: v2\n"

rails console:

>> h = {key1: 'v1', key2: 'v2'}
>> h.deep_stringify_keys.to_yaml
=> "---\nkey1: v1\nkey2: v2\n"
>> h.deep_stringify_keys.to_yaml.lines[1..-1].join
=> "key1: v1\nkey2: v2\n"
Adobe
  • 12,967
  • 10
  • 85
  • 126