数组平均路径

时间:2013-11-17 05:56:39

标签: ruby-on-rails ruby-on-rails-3.2

我想在我的控制器中显示我的数组'temp'(作为平均数组)的平均值,但不确定如何处理它。我可以在控制器中使用范围方法调用吗?或者我可以直接在控制器中进行操作吗?

控制器

def show
    @soiltemp = Soiltemp.find(params[:id])
    @data = CSV.open(@soiltemp.csv.path, :headers => true, :encoding => 'ISO-8859-1')
    dates = []
    temp = []
    @data.each do |row|  
     dates << row[1]
     temp << row[2].to_i
    end
    average = ?
    @graph = LazyHighCharts::HighChart.new('graph') do |f|
      f.title({ :text => @soiltemp.site + " Soil Temperatures" })
      f.options[:xAxis][:categories] =  dates
      f.options[:plotOptions] = {pointInterval: 7.day * 7000}
      f.series(:type => 'area', :name => 'Temperature', :data => temp, :color => '#00463f') 
      f.series(:type => 'spline',:name => 'Average', :data => average) 
    end

1 个答案:

答案 0 :(得分:12)

 average = temp.sum / temp.size.to_f