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

Version Path
dirwatch-0.0.9 vendor/bundle/ruby/2.5.0/gems/powerpack-0.1.1/lib/powerpack/enumerable/sum.rb
dirwatch-0.0.8 vendor/bundle/ruby/2.5.0/gems/powerpack-0.1.1/lib/powerpack/enumerable/sum.rb
dirwatch-0.0.6 vendor/bundle/ruby/2.3.0/gems/powerpack-0.1.1/lib/powerpack/enumerable/sum.rb
dirwatch-0.0.5 vendor/bundle/ruby/2.3.0/gems/powerpack-0.1.1/lib/powerpack/enumerable/sum.rb
dirwatch-0.0.4 vendor/bundle/ruby/2.3.0/gems/powerpack-0.1.1/lib/powerpack/enumerable/sum.rb
dirwatch-0.0.3 vendor/bundle/ruby/2.3.0/gems/powerpack-0.1.1/lib/powerpack/enumerable/sum.rb
dirwatch-0.0.2 vendor/bundle/ruby/2.3.0/gems/powerpack-0.1.1/lib/powerpack/enumerable/sum.rb
fluent-plugin-detect-memb-exceptions-0.0.2 vendor/bundle/ruby/2.0.0/gems/powerpack-0.1.1/lib/powerpack/enumerable/sum.rb
fluent-plugin-detect-memb-exceptions-0.0.1 vendor/bundle/ruby/2.0.0/gems/powerpack-0.1.1/lib/powerpack/enumerable/sum.rb
rubyjobbuilderdsl-0.0.2 vendor/bundle/ruby/2.1.0/gems/powerpack-0.0.9/lib/powerpack/enumerable/sum.rb
rubyjobbuilderdsl-0.0.1 vendor/bundle/ruby/2.1.0/gems/powerpack-0.0.9/lib/powerpack/enumerable/sum.rb
powerpack-0.1.1 lib/powerpack/enumerable/sum.rb
powerpack-0.1.0 lib/powerpack/enumerable/sum.rb
powerpack-0.0.9 lib/powerpack/enumerable/sum.rb
powerpack-0.0.8 lib/powerpack/enumerable/sum.rb
powerpack-0.0.7 lib/powerpack/enumerable/sum.rb