Method: Array#sum

Defined in:
lib/ruby/jruby_hack.rb

#sum(&block) ⇒ Object

Accumulate elements using the ‘+` method, optionally transforming them first using a block

Examples:

["a", "b", "cd"].sum             #=> "abcd"
["a", "b", "cd"].sum(&:length)   #=> 4


199
200
201
202
203
204
205
# File 'lib/ruby/jruby_hack.rb', line 199

def sum(&block)
  if block_given?
    tail.inject(yield(head)){|sum,e| sum + yield(e) }
  else
    tail.inject(head){|sum,e| sum + e }
  end
end