Skip navigation.

Syndicate

Syndicate content

User login

Annoying problem with trailing commas in values with YAML.rb

I’ve run into a very annoying and seemingly serious gotcha in the YAML.rb implementation that ships with Ruby 1.8.6 patchlevel 0 i386-mswin32. The problem is surfaced when you attempt to serialize a Ruby string value with a trailing comma. The YAML output looks OK, but if you read the YAML back in the resulting value isn’t what you expect.

You can repro with this Ruby code:

require 'yaml'
require 'test/unit'

# Repro the bug in the YAML serializer
class BrokenYamlTest  < Test::Unit::TestCase
    def test_broken_yaml
        data =  {
            'foo' => {
                'bar' => ['baz,']
            }
        }

        File.open('testdata.yaml', 'w') do |file|
            YAML::dump(data, file)
        end

        data2 = File.open('testdata.yaml', 'r') do |file|
            YAML::load(file)
        end 

        assert_equal(data['foo']['bar'][0],
            data2['foo']['bar'][0])
    end
end

When I run this code on my Windows machine with the aforementioned version of Ruby, I get this output:

    Loaded suite test_yaml_bug
    Started
    F
    Finished in 0.016 seconds.

      1) Failure:
    test_broken_yaml(BrokenYamlTest) [test_yaml_bug.rb:21]:
    <"baz,"> expected but was
    <"baz,\r">.

    1 tests, 1 assertions, 1 failures, 0 errors

As you can see, the baz, value is getting a carriage return (\r) character appended after it’s read back in from the YAML. Interestingly, if you serialize to/from a Ruby string instead of a file, this problem doesn’t surface.

I realize that the comma is a separator for list values in YAML, but I also don’t care. YAML.rb should provide round-trip consistency for something as simple as a trailing comma in a string literal.