Project

General

Profile

Actions

Patch #44736

open

Use Enumerable#sum instead of inject and explicit loops to calculate totals

Added by Go MAEDA 1 day ago.

Status:
New
Priority:
Normal
Assignee:
-
Category:
Code cleanup/refactoring

Description

Some totals in Redmine are still calculated with inject(0) or an explicit loop. These were written before Enumerable#sum became available in Ruby 2.4:

  • TimelogHelper#sum_hours
  • Redmine::Helpers::TimeReport#run (@total_hours)
  • Redmine::Helpers::Gantt#number_of_rows

The attached patch replaces them with Enumerable#sum, for example:

# Before
sum = 0
data.each do |row|
  sum += row['hours'].to_f
end
sum

# After
data.sum {|row| row['hours'].to_f}

Enumerable#sum states the intent directly, which makes the code easier to read.


Files

No data to display

Actions

Also available in: Atom PDF