5

我是 Rails 的新手,我遇到了一个错误,我似乎找不到问题所在。这是日志:

[32651:ERROR] 2012-10-09 13:46:52 :: comparison of Float with Float failed
[32651:ERROR] 2012-10-09 13:46:52 :: /home/sunny/backend/lib/analytics/lifetime.rb:45:in `each'
/home/sunny/backend/lib/analytics/lifetime.rb:45:in `max'
/home/sunny/backend/lib/analytics/lifetime.rb:45:in `max_growth'
/home/sunny/backend/lib/analytics/lifetime.rb:15:in `run'
/home/sunny/backend/lib/analytics/post_analytics.rb:102:in `lifetime'
/home/sunny/backend/lib/analytics/post_analytics.rb:27:in `run`run'
lib/endpoints/posts/index.rb:65:in `block in response'
lib/endpoints/posts/index.rb:62:in `each'
lib/endpoints/posts/index.rb:62:in `response'
/usr/local/lib/ruby/gems/1.9.1/gems/goliath-1.0.0.beta.1/lib/goliath/api.rb:163:in `call'
/usr/local/lib/ruby/gems/1.9.1/gems/goliath-1.0.0.beta.1/lib/goliath/rack/validation/required_param.rb:43:in `call'
/usr/local/lib/ruby/gems/1.9.1/gems/goliath-1.0.0.beta.1/lib/goliath/rack/params.rb:61:in `block in call'
/usr/local/lib/ruby/gems/1.9.1/gems/goliath-1.0.0.beta.1/lib/goliath/rack/validator.rb:40:in `safely'
/usr/local/lib/ruby/gems/1.9.1/gems/goliath-1.0.0.beta.1/lib/goliath/rack/params.rb:59:in `call'
/usr/local/lib/ruby/gems/1.9.1/gems/goliath-1.0.0.beta.1/lib/goliath/rack/async_middleware.rb:73:in `call'
/usr/local/lib/ruby/gems/1.9.1/gems/rack-1.4.1/lib/rack/content_length.rb:14:in `call'
/usr/local/lib/ruby/gems/1.9.1/gems/async-rack-0.5.1/lib/async_rack/async_callback.rb:114:in `call'
/usr/local/lib/ruby/gems/1.9.1/gems/async-rack-0.5.1/lib/async_rack/async_callback.rb:91:in `block in new'
/usr/local/lib/ruby/gems/1.9.1/gems/goliath-1.0.0.beta.1/lib/goliath/request.rb:163:in `call'
/usr/local/lib/ruby/gems/1.9.1/gems/goliath-1.0.0.beta.1/lib/goliath/request.rb:163:in `block in process'

每当我转到文件并找到该行时,都没有each,我无法理解问题出在哪里。任何人都可以帮忙吗?不仅仅是修复,我想了解这个问题。

这是文件:

class Lifetime
  attr_reader :values

  GROWTH_CUT_OFF = 0.10

  def initialize(engaged_users_values)
    @values = engaged_users_values
  end

  def run
    growths      = growth(values)

    return [0] if growths.uniq.first == 0 || growths.empty?

    max_growth   = max_growth(growths)
    dead_growth  = least_growth(growths, max_growth)

    time_diff = time_diff(values.last['end_time'],
                          values.first['end_time'])
    return [time_diff] unless dead_growth

    dead_loc     = growths.index(dead_growth)
    dead_value   = values[dead_loc]
    lifetime(values.first, dead_value)
  end

  def time_diff(last, first)
    in_minutes(parse_time(last)-parse_time(first))
  end

  def parse_time(time)
    DateTime.parse(time)
  end

  def growth(values)
    values.each_cons(2).map do |one, two|
      one_value = one['value'].to_f
      two_value = two['value']

      ((two_value-one_value)/(one_value)*100).round(2)
    end
  end

  def max_growth(growths)
    growths.max
  end

  def least_growth(growths, max_growth)
    growth_cut = growth_cut(max_growth)
    growths.select {|g| g <= growth_cut}.first
  end

  def growth_cut(max_growth)
    max_growth*GROWTH_CUT_OFF
  end

  def lifetime(first_value, last_value)
    first = parse_time(first_value['end_time'])
    last  = parse_time(last_value['end_time'])

    result = last-first
    [in_minutes(result)]
  end

  def in_minutes(time)
    time.to_f*24*60
  end
end
4

1 回答 1

15

如果两个浮点数之一是 NaN,那么您会收到此消息。因此,请防止出现 NaN 情况。

如果您发布错误的确切代码(max_growth 方法),我们可以为您提供更多信息

于 2012-10-09T14:35:55.297 回答