From bf09a24b04ed4b3ec4354b00711cdc5fe8c1b66a Mon Sep 17 00:00:00 2001 From: MAEDA Go Date: Tue, 10 Nov 2026 14:52:00 +0900 Subject: [PATCH] Use Enumerable#sum instead of inject and explicit loops to calculate totals The totals were accumulated with inject(0) or an explicit loop, written before Enumerable#sum was introduced in Ruby 2.4. Enumerable#sum states the intent directly, which makes the code easier to read. Co-Authored-By: Claude Opus 5.5 --- app/helpers/timelog_helper.rb | 6 +----- lib/redmine/helpers/gantt.rb | 2 +- lib/redmine/helpers/time_report.rb | 2 +- 3 files changed, 3 insertions(+), 7 deletions(-) diff --git a/app/helpers/timelog_helper.rb b/app/helpers/timelog_helper.rb index a03f9c701..9f0e7de95 100644 --- a/app/helpers/timelog_helper.rb +++ b/app/helpers/timelog_helper.rb @@ -65,11 +65,7 @@ module TimelogHelper end def sum_hours(data) - sum = 0 - data.each do |row| - sum += row['hours'].to_f - end - sum + data.sum {|row| row['hours'].to_f} end def format_criteria_value(criteria_options, value, html=true) diff --git a/lib/redmine/helpers/gantt.rb b/lib/redmine/helpers/gantt.rb index e3747761a..90e2bd5c2 100644 --- a/lib/redmine/helpers/gantt.rb +++ b/lib/redmine/helpers/gantt.rb @@ -122,7 +122,7 @@ module Redmine def number_of_rows return @number_of_rows if @number_of_rows - rows = projects.inject(0) {|total, p| total += number_of_rows_on_project(p)} + rows = projects.sum {|p| number_of_rows_on_project(p)} [rows, @max_rows].min end diff --git a/lib/redmine/helpers/time_report.rb b/lib/redmine/helpers/time_report.rb index 935ba6c20..0030a1500 100644 --- a/lib/redmine/helpers/time_report.rb +++ b/lib/redmine/helpers/time_report.rb @@ -77,7 +77,7 @@ module Redmine max = @hours.pluck('spent_on').max @to = max ? max.to_date : User.current.today - @total_hours = @hours.inject(0) {|s, k| s = s + k['hours'].to_f} + @total_hours = @hours.sum {|k| k['hours'].to_f} @periods = [] # Date#at_beginning_of_ not supported in Rails 1.2.x -- 2.55.0