Requesting an insight on reduce ruby code
问题 I started solving exercises in hackerrank in the enumerable section. The exercise asks to complete the sum method which takes an integer n and returns the sum to the n terms of the series. I found the solution from another source but I don't quite understand how the reduce works in this case and the output. def sum_terms(n) series = [] 1.upto(n) do |i| series.push(i ** 2 + 1) end series.reduce(0, :+) end puts sum_terms(5) # outputs 60 回答1: We can write this method as follows: def sum_terms(n)