Module: Polyfill::V2_4::Enumerable::Instance::Sum::Method

Defined in:
lib/polyfill/v2_4/enumerable/instance/sum.rb

Instance Method Summary collapse

Instance Method Details

#sum(init = 0) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/polyfill/v2_4/enumerable/instance/sum.rb', line 7

def sum(init = 0)
  acc =
    begin
      init.dup
    rescue TypeError
      init
    end

  each do |elem|
    acc += block_given? ? yield(elem) : elem
  end

  acc
end