我的Array
课堂上有以下方法:
class Array
def avg
if partial_include?(":")
avg_times
else
blank? and 0.0 or (sum.to_f/size).round(2)
end
end
def avg_times
avg_minutes = self.map do |x|
hour, minute = x.split(':')
total_minutes = hour.to_i * 60 + minute.to_i
end.inject(:+)/size
"#{avg_minutes/60}:#{avg_minutes%60}"
end
def partial_include?(search_term)
self.each do |e|
return true if e[search_term]
end
return false
end
end
这适用于常规数字数组,但可能存在我有一个时间数组的情况。
例如:[18:35, 19:07, 23:09]
无论如何要找出时间对象数组的平均值?