lib/open_classes/array/average.rb in tbpgr_utils-0.0.150 vs lib/open_classes/array/average.rb in tbpgr_utils-0.0.151

- old
+ new

@@ -1,17 +1,17 @@ -# encoding: utf-8 - -# Array -class Array - # return average - # - # === Example - # - # [*1..6].average # => 3.5 - # [1.5, 2.5].average # => 2.0 - # [*'a'..'z'].average # => raise TypeError - # - def average - fail TypeError, 'you have to use elements that is Numeric' if any? { |v|!v.is_a? Numeric } - reduce(0.0) { |r, v|r = r + v.to_f; r } / size - end -end +# encoding: utf-8 + +# Array +class Array + # return average + # + # === Example + # + # [*1..6].average # => 3.5 + # [1.5, 2.5].average # => 2.0 + # [*'a'..'z'].average # => raise TypeError + # + def average + fail TypeError, 'you have to use elements that is Numeric' unless all? { |v|v.is_a? Numeric } + sum.to_f / size + end +end