Yaml - appends a ' ' to a key but I do not understand why

Testscript. Run this to generate a /tmp/test.yml file and

populate it with the test data. This test data is stored

in @hash_data.

Here is the test.rb script:

require ‘pp’
require ‘yaml’

class YamlTesting

FILE = ‘/tmp/test.yml’

def initialize
@yaml = nil
@file = FILE
e ‘Testing from YamlTesting.rb now.’
@hash_data = { # our array test data, in hash form, please
# look at the key in it. It contains no ’ ’
‘abcdef-1.0.0’ => [
‘a’,‘b’,‘c’,‘d’,‘e’,‘f’
]
}
load_yaml # assign to @hash_data if found.
save_yaml
pp @hash_data
end
def load_yaml(i = @file)
if File.exist? i
e ‘File ‘+i+’ exists.’
@hash_data = YAML.load(File.new(i))
end
end
def save_yaml # save the yaml file somewhere. You will
# see that the key has an appended ’ ’
# in the resulting file.
e 'Saving yaml Data into '+@file
File.open(@file, ‘w+’) {|f| YAML.dump(@hash_data, f )}
end
alias e puts
end
YamlTesting.new

This will generate a yaml file like this:


abcdef-1.0.0:

  • a
  • b
  • c
  • d
  • e
  • f

The thing that disturbs me is that the string
"abcdef-1.0.0: " has an appended ’ '.

If you look at the datastructure in the ruby script
that generates this yaml file, the array name is:
“abcdef-1.0.0:” without appended ’ '.

It probably makes no difference, but I am still
wondering why it does so. :frowning:

PS: The pasted format does not have the trailing ’ ’
but if you generate the yaml file via the ruby
script, then the result .yml file will have the
trailing ’ ’

Marc H. wrote in post #1009420:


abcdef-1.0.0:

  • a
  • b
  • c
  • d
  • e
  • f

The thing that disturbs me is that the string
"abcdef-1.0.0: " has an appended ’ '.

It also has an appended line feed, and it is preceded by some dashes and
a line feed. Do those things bother you?

Do the dashes in front of the array values bother you?

Are you aware that files are one long string in the first place? Does
that bother you?

Marc H. wrote in post #1009420:

The thing that disturbs me is that the string
"abcdef-1.0.0: " has an appended ’ '.

The important thing is, if you read the YAML file back in, do you get
the same data structure back?

The YAML spec allows spaces in certain places.

Having said that: if you’re running ruby 1.8 with its default YAML
implementation (syck), then it’s full of bugs, especially to do with
strings with leading/trailing whitespace. So I would not recommend it
for general-purpose object serialisation. However, as a way of reading
in a config file it’s fine.