Logo

Programming-Idioms

History of Idiom 54 > diff from v21 to v22

Edit summary for version 22 by steenslag:
[Ruby] Use new feature

Version 21

2016-04-07, 07:03:57

Version 22

2017-03-26, 22:00:59

Idiom #54 Compute sum of integers

Calculate the sum s of integer list x.

Idiom #54 Compute sum of integers

Calculate the sum s of integer list x.

Code
s = x.reduce(:+)
s = x.reduce(0) { |memo, elem| memo + elem }
Code
s = x.sum
Comments bubble
Works since Ruby 2.4. Older versions:
s = x.reduce(:+)
Doc URL
http://ruby-doc.org/core-2.2.3/Enumerable.html#method-i-reduce
Doc URL
http://ruby-doc.org/core-2.2.3/Enumerable.html#method-i-reduce