因为显然require 'date'
没有包含小时或秒等方法:
undefined method `hours' for 5:Fixnum (NoMethodError)
我有什么遗漏吗?5.seconds只能在Rails中使用吗?如果是这样的话,在Ruby脚本中使用它需要哪个require语句呢?
虽然这是一个老问题,但是对于像我这样的谷歌搜索者来说仍然有参考价值:
require 'active_support/time'
无论如何,对于宝石版本3.2.11来说。
以下方法适用于我
irb
>> require 'active_support'
=> true
>> 5.hours
=> 18000 seconds
根据你的环境和Rails版本,你可能需要在require 'active_support'
行之前执行require 'rubygems'
。
如果你使用较旧的Rails版本,则可能还需要执行require 'activesupport'
而不是active_support
。
gem 'active_support/time'
添加到gemfile后,我收到了以下错误信息:Could not find gem 'active_support/time' in any of the gem sources listed in your Gemfile.
- stevecrails
的ruby
项目,请确保使用命令gem install rails
将rails
安装到项目中,因为该扩展是一个rails
扩展。然后在您的项目中添加require 'active_support/time'
。 - Promise Preston