Ruby runt精度错误

时间:2013-02-13 18:37:36

标签: ruby ruby-on-rails-3 gem

我在rails 3升级(ruby 1.9.3)中使用runt gem,并收到此错误:undefined method precision'为nil:NilClass`。

这是如何在irb / pry上复制它:

> require 'runt'
=> true
> start_at = Runt::PDate.min(2009,1,1, 0, 0)
=> #<Runt::PDate: 2009-01-01T00:00:00+00:00 ((2454833j,0s,0n),+0s,2299161j)>
> end_at = Runt::PDate.min(2009,1,1, 1, 30)
=> #<Runt::PDate: 2009-01-01T01:30:00+00:00 ((2454833j,5400s,0n),+0s,2299161j)>
> runt_pattern = Runt::EveryTE.new(start_at, 600)
=> every 600 minutes starting Thu Jan  1 00:00:00 2009
> date_range = Runt::DateRange.new(start_at, end_at)
=> #<Runt::PDate: 2009-01-01T00:00:00+00:00 ((2454833j,0s,0n),+0s,2299161j)>
..
#<Runt::PDate: 2009-01-01T01:30:00+00:00 ((2454833j,5400s,0n),+0s,2299161j)>
> schedule = Runt::Schedule.new
=> #<Runt::Schedule:0x007fd46cad56b8 @elems={}>
> schedule.add 'events', runt_pattern
=> every 600 minutes starting Thu Jan  1 00:00:00 2009
> schedule.dates('events', date_range)
NoMethodError: undefined method `precision' for nil:NilClass
from /Users/ttt/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/runt-0.7.0/lib/runt/dprecision.rb:121:in `<=>'

有人对此有所了解吗?

似乎是由这一行触发的:https://github.com/texel/runt/blob/master/lib/runt/schedule.rb#L28,但仍试图弄清楚如何深入挖掘代码。

我注意到有一些更新的runt版本,例如https://github.com/craigw/runt,但它们似乎也无法解决问题。

它适用于ree-1.8.7-2011.03上的rails2,并返回[Thu, 01 Jan 2009 00:00:00 +0000]

编辑:添加了rails2返回的内容。

1 个答案:

答案 0 :(得分:0)

您的问题似乎在EveryTE class。在initialize方法中有一个可选的精度参数。

class EveryTE

  include TExpr

  def initialize(start,n,precision=nil)
    @start=start
    @interval=n
    # Use the precision of the start date by default
    @precision=precision || @start.date_precision
  end

  def include?(date)
    i=DPrecision.to_p(@start,@precision)
    d=DPrecision.to_p(date,@precision)
    while i<=d
      return true if i.eql?(d)
      i=i+@interval
    end
    false
  end

  def to_s
    "every #{@interval} #{@precision.label.downcase}s starting #{Runt.format_date(@start)}"
  end

end

我会尝试提供精确度而不是接受默认值。

> runt_pattern = Runt::EveryTE.new(start_at, 600,Runt::DPrecision::Precision.sec)

以下是精确度的完整列表(在DPrecision class中):

Precision.year
Precision.month
Precision.week
Precision.day
Precision.hour
Precision.min
Precision.sec
Precision.millisec