ruby – 为什么YAML解析器(syck和psych)的行为不同?

看看这个案例:

ruby 1.9.2p0(2010-08-18修订版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"

当我使用心理解析器并且我需要格式化一个模糊地类似于日期的字符串时,它会引发异常,因为他认为这是一个日期字符串.使用syck不会发生此问题.

任何人都有任何想法?

最佳答案 Psych几乎独立于Syck,我不认为Psych的作者有兴趣与Syck成为
bug-compatible.我认为他们无论如何都会实现不同版本的YAML规范.

一种解决方法应该是将字符串放在类似日期的数字上.

点赞