2012-10-09 14 views
5

Sono nuovo alle rotaie e sto ricevendo un errore e non riesco a trovare il problema. Ecco il log:32651: confronto ERROR di Float with Float failed ruby ​​

[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' 

Ogni volta che vado al file e scoprire che fila, non c'è each, e non riesco a capire dove sia il problema. Qualcuno può aiutare? Più che una correzione, voglio capire il problema.

Ecco il file:

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 

risposta

12

Se uno dei due galleggianti è NaN quindi si ottiene questo messaggio. Quindi, previeni il caso NaN.

Siamo in grado di dare più informazioni, se si registra il codice esatto dell'errore (il metodo max_growth)

+0

crescita def (valori) values.each_cons (2) .map fanno | uno, due | one_value = one [ 'valore']. To_f two_value = due [ 'valore'] se one_value == NaN one_value == 0 altro ((two_value-one_value)/(one_value) * 100) .round (2) fine fine fine def max_growth (escrescenze) growths.max fine –

+0

Questo è illeggibile. Crea una nuova domanda per sapere come risolvere questa specifica cosa, dato che nessuno guarderà in una domanda già risposta. –