【发布时间】:2012-02-04 12:24:08
【问题描述】:
看这个案例:
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"
当我使用 psych 解析器并且我需要格式化一个模糊地类似于日期的字符串时,它会引发异常,因为他认为这是一个日期字符串。使用syck不会出现这个问题。
有人知道吗?
【问题讨论】:
-
看起来这个问题最近在 Ruby head 中得到了解决:github.com/ruby/ruby/commit/…。
-
我有一个类似的问题,看起来像浮点数的东西被解析为浮点数,除非你在它周围加上字符串:stackoverflow.com/questions/8708113/…