Sha256: 951e64d952263703654bcac86c7ef3d38b8f38043f015c17d79f18084ee4e194
Contents?: true
Size: 637 Bytes
Versions: 16
Compression:
Stored size: 637 Bytes
Contents
unless Enumerable.method_defined? :sum module Enumerable # Sums up elements of a collection by invoking their `+` method. # Most useful for summing up numbers. # # @param default [Object] an optional default return value if there are no elements. # It's nil by default. # @return The sum of the elements or the default value if there are no # elements. # # @example # [1, 2, 3].sum #=> 6 # ["a", "b", "c"].sum #=> "abc" # [[1], [2], [3]].sum #=> [1, 2, 3] # [].sum #=> nil # [].sum(0) #=> 0 def sum(default = nil) reduce(&:+) || default end end end
Version data entries
16 entries across 16 versions & 4 rubygems