如果有时间戳,则Ruby eval方法会出错

时间:2013-12-31 08:32:07

标签: ruby-on-rails ruby timestamp

我正在尝试使用字符串制作哈希:

hash = eval({"name"=>["name", "changed"], "updated_at"=>[nil, Tue, 31 Dec 2013 09:39:29 EET +02:00]})

但是会出现以下错误:

SyntaxError: (irb):19: syntax error, unexpected tCONSTANT, expecting ']'
..."updated_at"=>[nil, Tue, 31 Dec 2013 09:39:29 EET +02:00]})
...                               ^
(irb):19: Invalid octal digit
..._at"=>[nil, Tue, 31 Dec 2013 09:39:29 EET +02:00]})
...                               ^

我该如何处理?谢谢大家。

1 个答案:

答案 0 :(得分:2)

将此代码编写为: -

hash = eval({"name"=>["name", "changed"], "updated_at"=>[nil, "Tue, 31 Dec 2013 09:39:29 EET +02:00"]})

hash = eval({"name"=>["name", "changed"], "updated_at"=>[nil, Date.today]})