Kirill Zhuravlov Kirill Zhuravlov - 16 days ago 6
Ruby Question

Is there a good way to calculate sum of range elements in ruby

What is the good way co calculate sum of range?

Input

4..10


Output

4 + 5 + 6 + 7 + 8 + 9 + 10 = 49

Answer

You can use Enumerable methods on Range objects, in this case use Enumerable#inject:

(4..10).inject(:+)
 #=> 49