时间值数组的Ruby平均值(fixnums) [英] Ruby average value of array of time values (fixnums)
本文介绍了时间值数组的Ruby平均值(fixnums)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
要获取平均持续时间,持续时间的格式为1.day,3.months,2weeks。.
Looking to get the average duration, where duration is in the form of 1.day, 3.months, 2.weeks format..
# provided array
a = [1.day, 3.days, 1.week, 4.days]
# desired output
a.average = "3 days"
无论如何,我尝试以几秒钟为单位的结果就是输出。例如:
Any way I have tried results in a number of seconds being the output.. for instance:
a.inject(:+) = "15 days"
a.inject(:+) / a.size = 324000
我看过《 Linguistics gem》,但它只将值输出为数字(十二万四千二百)
I've looked at the Linguistics gem, but it only outputs the value as a number (three hundred and twenty four thousand)
推荐答案
def average_days(a)
seconds = a.inject(:+) / a.size
minutes = seconds / 60
days = (minutes / 1440).round
"#{days} days"
end
这篇关于时间值数组的Ruby平均值(fixnums)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文