Method: Array#sum
- Defined in:
- lib/array_statistics.rb
#sum(&value_block) ⇒ Object
Returns the sum of all the values in this array.
Like most methods in this package, this method takes an optional block that defines the “value” of the objects in the array. This block can be safely skipped if the array contains numbers.
208 209 210 211 212 213 214 215 |
# File 'lib/array_statistics.rb', line 208 def sum(&value_block) # :yields: element value_block = proc{|element| element} unless block_given? s = 0 each do |element| s = s + value_block.call(element) end s end |