TL;DR:一种可能的解决方案
def compute_average(*values)
# This makes sure arrays get flattened to a single array.
values.flatten!
# Throws away all nil values passed as arguments.
values.reject!(&:nil?)
# Throws away all non-numeric values.
# This includes trashing strings that look like numbers, like "12".
values.keep_if{ |v| v.is_a? Numeric }
total = values.sum.to_f
return Float::NAN if total.zero?
# I'm not sure what this business is
# average = [a, 2*b, 3*c, 4*d, 5*e].sum / total
# but it can be translated to
average = values.each_with_index.map{ |v,i| v*(i+1) }.sum / total
average.round(2)
end
这可以防止所有情况:
compute_average(1,2,3,4,5)
=> 3.67
compute_average(0,0,0,0,0)
=> NaN
compute_average(1,2,nil,4,5)
=> 3.08
compute_average(1,2,"string",4,5)
=> 3.08
compute_average(1)
=> 1.0
compute_average([1,2,3,4,5])
=> 3.67
compute_average
=> NaN
原始功能:
def compute_average(a,b,c,d,e)
total = [a,b,c,d,e].sum.to_f
average = [a, 2*b, 3*c, 4*d, 5*e].sum / total
average.round(2)
end
考虑检查零:
def compute_average(a,b,c,d,e)
total = [a,b,c,d,e].sum.to_f
return if total.zero?
average = [a, 2*b, 3*c, 4*d, 5*e].sum / total
average.round(2)
end
此更改仅可防止一种情况:
compute_average(1,2,3,4,5)
# => 3.67
compute_average(0,0,0,0,0)
# => nil
compute_average(1,2,nil,4,5)
# => TypeError: NilClass can't be coerced into Fixnum
compute_average(1,2,"string",4,5)
# => TypeError: String can't be coerced into Fixnum
compute_average(1)
# => ArgumentError: wrong number of arguments calling `compute_average` (1 for 5)
compute_average([1,2,3,4,5])
# => ArgumentError: wrong number of arguments calling `compute_average` (1 for 5)
compute_average
# => ArgumentError: wrong number of arguments calling `compute_average` (0 for 5)
考虑使用内联rescue
def compute_average(a,b,c,d,e)
total = [a,b,c,d,e].sum.to_f
average = [a, 2*b, 3*c, 4*d, 5*e].sum / total rescue 0
average.round(2)
end
此更改仅可防止一种情况,还:
compute_average(1,2,3,4,5)
# => 3.67
compute_average(0,0,0,0,0)
# => NaN
compute_average(1,2,nil,4,5)
# => TypeError: NilClass can't be coerced into Fixnum
compute_average(1,2,"string",4,5)
# => TypeError: String can't be coerced into Fixnum
compute_average(1)
# => ArgumentError: wrong number of arguments calling `compute_average` (1 for 5)
compute_average([1,2,3,4,5])
# => ArgumentError: wrong number of arguments calling `compute_average` (1 for 5)
compute_average
# => ArgumentError: wrong number of arguments calling `compute_average` (0 for 5)
使用内联rescue
有另一个后果。考虑这个错字:
def compute_average(a,b,c,d,e)
total = [a,b,c,d,e].sum.to_f
average = [a, 2*b, 3*c, 4*d, 5*e].smu / total rescue 0
# ^^^
average.round(2)
end
compute_average(1,2,3,4,5)
# => 0.0
compute_average(0,0,0,0,0)
# => 0.0
考虑使用一个rescue
def compute_average(a,b,c,d,e)
total = [a,b,c,d,e].sum.to_f
average = [a, 2*b, 3*c, 4*d, 5*e].sum / total
average.round(2)
rescue ZeroDivisionError
0.0
end
这更好,因为它不会隐藏错误,但可以防止与上述倾斜相同的情况rescue
。
另一个我称之为正常平均计算的版本
作为旁注,我熟悉的平均操作是使用总数/计数计算的,所以这里有一个版本可以做到这一点。
def compute_average(*values)
# This makes sure arrays get flattened to a single array.
values.flatten!
# Throws away all nil values passed as arguments.
values.reject!(&:nil?)
# Throws away all non-numeric values.
# This includes trashing strings that look like numbers, like "12".
values.keep_if{ |v| v.is_a? Numeric }
total = values.sum.to_f
count = values.count
return Float::NAN if count.zero?
total / count
end
这可以防止所有情况:
compute_average(1,2,3,4,5)
=> 3.0
compute_average(0,0,0,0,0)
=> 0.0
compute_average(1,2,nil,4,5)
=> 3.0
compute_average(1,2,"string",4,5)
=> 3.0
compute_average(1)
=> 1.0
compute_average([1,2,3,4,5])
=> 3.0
compute_average
=> NaN