简体   繁体   English

找出参数的平均值

[英]rails to find the average of an parameters

I am trying to calculate the average of parameters by passing them to an array but I am getting the following error. 我正在尝试通过将参数传递给数组来计算参数的平均值,但出现以下错误。 So I have doubt am I doing it right? 所以我怀疑我做对了吗?

Here is my code 这是我的代码

arr= @r1+@r2+@r3+@r4+@r5
    @current_user_satisfaction= arr.inject{ |sum, el| sum + el }.to_i / arr.size

Where I am getting r1, r2,r3,r4 from the DB. 我从数据库中得到r1,r2,r3,r4。 I am getting the following error 我收到以下错误

: syntax error, unexpected tIDENTIFIER, expecting keyword_end @current_user satisfaction= arr.inject{ |sum, el| sum +... ^

What was my problem where is my error. 我的问题是什么,我的错误在哪里。 I actually started learning ROR very recently. 我实际上是最近才开始学习ROR的。

This what I am writing to do 这是我写的

I have as set of variables which I am taking from db based up user_id and company_id the variables are 我有一组变量,这些变量是我从db根据user_id和company_id获取的,这些变量是

  @r1=company_rating.collect(&:r1)
  @r2=company_rating.collect(&:r2)
  @r3=company_rating.collect(&:r3)
  @r4=company_rating.collect(&:r4)
And I wanted to find the avg of all those variables. 

So I am doing it like arr = [@r1,@r2,@r3,®r4] @current-user_satisfaction= arr.sum.compact /arr.size But I am getting an error / is undefined and Why I am doing compact is because I have sum of the nil values in that 所以我这样做就像arr = [@ r1,@ r2,@ r3,®r4] @ current-user_satisfaction = arr.sum.compact /arr.size但我遇到错误/未定义,为什么我要紧凑是因为我有零值的总和

So please help how do this. 因此,请帮助执行此操作。

This is not an array! 这不是数组!

arr= @r1+@r2+@r3+@r4+@r5

This is a sum. 这是一笔数目。 Should be arr= [@r1,@r2,@r3,@r4,@r5] Additionally you missed dot between @current_user and satisfaction . 应该是arr= [@r1,@r2,@r3,@r4,@r5]另外,您还错过了@current_usersatisfaction之间的点。

Also it is not as it should be done in rails. 也并非如此,因为它应该在导轨中完成。 Could you show us how those variables are set? 您能告诉我们这些变量是如何设置的吗?

First, You need to create the array like this 首先,您需要像这样创建数组

arr= [@r1,@r2,@r3,@r4,@r5]

The error you are getting is about the . 您遇到的错误是关于. dot you missed between @current_user and satisfaction . 您在@current_usersatisfaction之间错过的点。 Change it accordingly and you will get it working. 相应地对其进行更改,即可使其正常工作。

As far as I think, you don't need an array there. 据我认为,您在那里不需要数组。 if you are doing, arr= @r1+@r2+@r3+@r4+@r5 , it is already calculating the sum of values, why you need to put them to array and go for a loop. 如果您这样做, arr= @r1+@r2+@r3+@r4+@r5 ,它已经在计算值的总和,为什么需要将它们放入数组并循环。

However, If you still want to use array, instead of arr.inject{ |sum, el| sum + el } 但是,如果仍然要使用数组,请使用arr.inject{ |sum, el| sum + el }代替数组 arr.inject{ |sum, el| sum + el } , you can use arr.reduce('+') , which does the same thing of calculating sum of all array values. arr.inject{ |sum, el| sum + el } ,可以使用arr.reduce('+') ,它对计算所有数组值的和执行相同的操作。 This has nothing to do with your problem, but makes your code more understandable. 这与您的问题无关,但是使您的代码更易于理解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM