Why the difference in behavior of YAML parsers (syck and psych)?

3.1k views Asked by At

Look at this case:

ruby 1.9.2p0 (2010-08-18 revision 29036) [i686-linux]

irb(main):006:0> YAML::ENGINE.yamler = "psych"
=> "psych"

irb(main):007:0> '8902-20-13'.to_yaml
ArgumentError: invalid date
from /usr/local/lib/ruby/1.9.1/date.rb:1022:in `new_by_frags'
from /usr/local/lib/ruby/1.9.1/date.rb:1046:in `strptime'
from /usr/local/lib/ruby/1.9.1/psych/scalar_scanner.rb:45:in `tokenize'
from /usr/local/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:191:in `visit_String'
from /usr/local/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:63:in `accept'
from /usr/local/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:36:in `<<'
from /usr/local/lib/ruby/1.9.1/psych.rb:165:in `dump'
from /usr/local/lib/ruby/1.9.1/psych/core_ext.rb:13:in `psych_to_yaml'

irb(main):008:0> YAML::ENGINE.yamler = "syck"
=> "syck"

irb(main):009:0> '8902-20-13'.to_yaml
=> "--- \"8902-20-13\"\n"

When I'm using the psych parser and I need to format a string that vaguely resembles a date, it throws an exception, because he thinks this is a date string. Using syck this problem does not occur.

Anyone have any idea?

2

There are 2 answers

0
Daniel Tamiosso On BEST ANSWER

As @matt had mentioned, the answer is this pull request: https://github.com/ruby/ruby/commit/9f688d53c2b5af5960d1e8d8fb09b26aa9d8b5f9

0
Andrew Grimm On

Psych is pretty much independent of Syck, and I don't think authors of Psych are interested in being bug-compatible with Syck. I think they implement different versions of the YAML specification anyway.

One workaround should be to put strings around the date-like-numbers.