看看这个案例:为什么YAML解析器(syck和psych)的行为有所不同?
红宝石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这个问题不会发生。
任何人有什么想法?
貌似这已经在Ruby中头部最近解决:https://github.com/ruby/ruby/commit/9f688d53c2b5af5960d1e8d8fb09b26aa9d8b5f9。 – matt 2012-01-07 00:21:48
我有一个类似的问题,看起来像一个浮动的东西被解析为一个浮动,除非你把它放在字符串:http://stackoverflow.com/questions/8708113/is-it-safe-to-use-漂浮在yaml-hashes-with-ruby – 2012-01-08 23:06:19
很酷@matt,谢谢! – 2012-01-09 16:06:17