Ruby 1.8.7添加10分钟到时间对象

Gus*_*Gus 0 ruby

所以我是ruby的新手,现在我仍然坚持使用1.8.7.我正在尝试在阅读文档后添加10分钟到一个时间对象,我没有这样的运气.

require "time"

curr_time = Time.now.strftime("%d-%b-%Y %H:%M:%S")
curr_time = curr_time + 600
Run Code Online (Sandbox Code Playgroud)

然后我得到一个错误说

'+':无法将Fixnum转换为String(TypeError)

有任何想法吗?

Mar*_*pka 6

这是因为在你的curr_time变量中你有strftime方法调用的结果,而String不是Time实例.要添加10分钟,您可以执行以下操作:

curr_time = Time.now
(curr_time + 600).strftime("%d-%b-%Y %H:%M:%S")
Run Code Online (Sandbox Code Playgroud)