diff --git a/.rubocop.common.yml b/.rubocop.common.yml index d896ed7ffa4..57bb3122f84 100644 --- a/.rubocop.common.yml +++ b/.rubocop.common.yml @@ -96,6 +96,8 @@ Performance/StringInclude: Severity: error Performance/StringReplacement: Severity: error +Performance/Sum: + Severity: error Performance/TimesMap: Severity: error diff --git a/app/models/conditional_release/stats.rb b/app/models/conditional_release/stats.rb index b152890efb7..840a6ff7237 100644 --- a/app/models/conditional_release/stats.rb +++ b/app/models/conditional_release/stats.rb @@ -140,7 +140,7 @@ module ConditionalRelease new_scores = Array.wrap(new_score_or_scores).compact return unless old_score && new_scores.present? - average = new_scores.reduce(&:+) / new_scores.length + average = new_scores.sum / new_scores.length percentage_points_improvement = average - old_score return 1 if percentage_points_improvement >= 0.03 return -1 if percentage_points_improvement <= -0.03 diff --git a/app/models/group_category.rb b/app/models/group_category.rb index 5adccfaa668..968ad9edace 100644 --- a/app/models/group_category.rb +++ b/app/models/group_category.rb @@ -474,7 +474,7 @@ class GroupCategory < ActiveRecord::Base counts = User.joins(:not_ended_enrollments) .where(enrollments: { course_id: context, type: 'StudentEnrollment' }) .distinct('user_id').group('course_section_id').count - @create_group_count = counts.values.map { |count| count / @create_group_member_count.to_f }.map(&:ceil).sum + @create_group_count = counts.values.map { |count| count / @create_group_member_count.to_f }.sum(&:ceil) else (unassigned_users.to_a.length.to_f / @create_group_member_count).ceil end @@ -660,7 +660,7 @@ class GroupCategory < ActiveRecord::Base end @group_distributions[section_id] = dist end - if @group_distributions.values.map(&:count).sum != @groups.count || @group_distributions.any? { |k, v| v.sum != user_counts[k] } + if @group_distributions.values.sum(&:count) != @groups.count || @group_distributions.any? { |k, v| v.sum != user_counts[k] } raise "user/group count mismatch" # we should make sure this works before going any further end diff --git a/app/models/media_object.rb b/app/models/media_object.rb index 74adc996f9d..19bb166afef 100644 --- a/app/models/media_object.rb +++ b/app/models/media_object.rb @@ -227,7 +227,7 @@ class MediaObject < ActiveRecord::Base self.max_size = [self.max_size || 0, asset[:size].to_i].max end end - self.total_size = [self.max_size || 0, assets.map { |a| (a[:size] || 0).to_i }.sum].max + self.total_size = [self.max_size || 0, assets.sum { |a| (a[:size] || 0).to_i }].max self.save ensure_attachment self.data diff --git a/app/models/quizzes/quiz_outcome_result_builder.rb b/app/models/quizzes/quiz_outcome_result_builder.rb index e82b8ae1d9a..e2318da05d5 100644 --- a/app/models/quizzes/quiz_outcome_result_builder.rb +++ b/app/models/quizzes/quiz_outcome_result_builder.rb @@ -121,8 +121,7 @@ module Quizzes end result.possible = cached_questions_and_answers.map(&:first) - .map { |h| h['points_possible'] } - .inject(:+) + .sum { |h| h['points_possible'] } # do not create a result if no points are possible. if result.possible == 0 @@ -132,8 +131,7 @@ module Quizzes end result.score = cached_questions_and_answers.map(&:last) - .map { |h| h[:points] } - .inject(:+) + .sum { |h| h[:points] } result.calculate_percent! result.mastery = determine_mastery(result, alignment) diff --git a/app/models/quizzes/quiz_regrader/submission.rb b/app/models/quizzes/quiz_regrader/submission.rb index d5c4181a039..e0c6f9d7592 100644 --- a/app/models/quizzes/quiz_regrader/submission.rb +++ b/app/models/quizzes/quiz_regrader/submission.rb @@ -43,7 +43,7 @@ class Quizzes::QuizRegrader::Submission def rescored_submission previous_score = submission.score_before_regrade || submission.score - submission.score += answers_to_grade.map(&:regrade!).inject(&:+) || 0 + submission.score += answers_to_grade.sum(&:regrade!) submission.score_before_regrade = previous_score submission.quiz_data = regraded_question_data submission diff --git a/app/models/quizzes/quiz_statistics/item_analysis/summary.rb b/app/models/quizzes/quiz_statistics/item_analysis/summary.rb index 8814369056b..032cd56ccb9 100644 --- a/app/models/quizzes/quiz_statistics/item_analysis/summary.rb +++ b/app/models/quizzes/quiz_statistics/item_analysis/summary.rb @@ -115,7 +115,7 @@ class Quizzes::QuizStatistics::ItemAnalysis::Summary size = items.size if size > 1 && variance != 0 - variance_sum = items.map(&:variance).sum + variance_sum = items.sum(&:variance) size / (size - 1.0) * (1 - (variance_sum / variance)) else nil diff --git a/app/models/quizzes/quiz_statistics/student_analysis.rb b/app/models/quizzes/quiz_statistics/student_analysis.rb index 4a1798b8dd0..4b399c853f3 100644 --- a/app/models/quizzes/quiz_statistics/student_analysis.rb +++ b/app/models/quizzes/quiz_statistics/student_analysis.rb @@ -420,7 +420,7 @@ class Quizzes::QuizStatistics::StudentAnalysis < Quizzes::QuizStatistics::Report question = Quizzes::QuizQuestion::Base.from_question_data(question).stats(responses) none = { - :responses => question[:responses] - question[:answers].map { |a| a[:responses] || 0 }.sum, + :responses => question[:responses] - question[:answers].sum { |a| a[:responses] || 0 }, :id => "none", :weight => 0, :text => I18n.t('statistics.no_answer', "No Answer"), diff --git a/app/models/rubric.rb b/app/models/rubric.rb index c3ba1dfe77a..8e326e1d2c1 100644 --- a/app/models/rubric.rb +++ b/app/models/rubric.rb @@ -401,7 +401,7 @@ class Rubric < ActiveRecord::Base end def total_points_from_criteria(criteria) - criteria.reject { |c| c[:ignore_for_scoring] }.map { |c| c[:points] }.reduce(:+) + criteria.reject { |c| c[:ignore_for_scoring] }.sum { |c| c[:points] } end # undo innocuous changes introduced by migrations which break `will_change_with_update?` diff --git a/gems/multipart/lib/multipart/sequenced_stream.rb b/gems/multipart/lib/multipart/sequenced_stream.rb index 25d706116c3..6f2e6c6b895 100644 --- a/gems/multipart/lib/multipart/sequenced_stream.rb +++ b/gems/multipart/lib/multipart/sequenced_stream.rb @@ -24,7 +24,7 @@ module Multipart end def size - @streams.map(&:size).sum + @streams.sum(&:size) end def read(size = nil, outbuf = +"") diff --git a/lib/score_statistics_generator.rb b/lib/score_statistics_generator.rb index 993444760de..4d3c5ac88ef 100644 --- a/lib/score_statistics_generator.rb +++ b/lib/score_statistics_generator.rb @@ -136,7 +136,7 @@ class ScoreStatisticsGenerator return end - average = current_scores.map(&:to_d).sum / BigDecimal(score_count) + average = current_scores.sum(&:to_d) / BigDecimal(score_count) # This is a safeguard to avoid blowing up due to database storage which is set to be a decimal with a precision of 8 # and a scale of 2. And really, what are you even doing awarding 1,000,000% or over in a course? diff --git a/lib/simple_stats.rb b/lib/simple_stats.rb index cf187190ac1..c826d424022 100644 --- a/lib/simple_stats.rb +++ b/lib/simple_stats.rb @@ -23,7 +23,7 @@ module SimpleStats divisor = type == :population ? items.length : items.length - 1 mean = items.sum / items.length.to_f - sum = items.map { |item| (item - mean)**2 }.sum + sum = items.sum { |item| (item - mean)**2 } (sum / divisor).to_f end module_function :variance diff --git a/lib/sis/csv/import_refactored.rb b/lib/sis/csv/import_refactored.rb index fc1b14f56c3..a78e7f04154 100644 --- a/lib/sis/csv/import_refactored.rb +++ b/lib/sis/csv/import_refactored.rb @@ -211,7 +211,7 @@ module SIS def update_progress completed_count = @batch.parallel_importers.where(workflow_state: "completed").count - current_progress = [(completed_count.to_f * 100 / @parallel_importers.values.map(&:count).sum).round, 99].min + current_progress = [(completed_count.to_f * 100 / @parallel_importers.values.sum(&:count)).round, 99].min SisBatch.where(:id => @batch).where("progress IS NULL or progress < ?", current_progress).update_all(progress: current_progress) end @@ -321,7 +321,7 @@ module SIS end end @parallel_importers.each do |type, importers| - @batch.data[:counts][type.to_s.pluralize.to_sym] = importers.map(&:rows_processed).sum + @batch.data[:counts][type.to_s.pluralize.to_sym] = importers.sum(&:rows_processed) end finish end diff --git a/lib/tasks/canvas.rake b/lib/tasks/canvas.rake index 25a503347d2..e22b6ac924a 100644 --- a/lib/tasks/canvas.rake +++ b/lib/tasks/canvas.rake @@ -81,7 +81,7 @@ unless $canvas_tasks_loaded end end - combined_time = batch_times.reduce(:+) + combined_time = batch_times.sum puts( "Finished compiling assets in #{real_time.round(2)}s. " + diff --git a/spec/migrations/fix_points_possible_sums_in_quizzes_spec.rb b/spec/migrations/fix_points_possible_sums_in_quizzes_spec.rb index f6a07084da1..c0fed6fda62 100644 --- a/spec/migrations/fix_points_possible_sums_in_quizzes_spec.rb +++ b/spec/migrations/fix_points_possible_sums_in_quizzes_spec.rb @@ -38,7 +38,8 @@ describe 'FixPointsPossibleSumsInQuizzes' do before do # creates rounding error just like the real thing! - points = Array.new(question_count) { question_points }.inject(:+) # .sum uses the native ruby 2.4 sum if the first element is numeric in rails 5.1 ...and doesn't quite give the same answer :/ + # .sum uses the native ruby 2.4 sum if the first element is numeric in rails 5.1 ...and doesn't quite give the same answer :/ + points = Array.new(question_count) { question_points }.inject(:+) # rubocop:disable Performance/Sum allow(Quizzes::Quiz).to receive(:count_points_possible).and_return(points) end diff --git a/spec/models/group_category_spec.rb b/spec/models/group_category_spec.rb index 80edaa72b3a..b1337e5bf9b 100644 --- a/spec/models/group_category_spec.rb +++ b/spec/models/group_category_spec.rb @@ -727,7 +727,7 @@ def assert_random_group_assignment(category, course, initial_spread, result_spre end # set up course users - user_count = result_spread.inject(:+) + expected_leftover_count + user_count = result_spread.sum + expected_leftover_count course_users = create_users_in_course(course, user_count, return_type: :record) # set up initial spread diff --git a/spec/models/quizzes/quiz_statistics/item_analysis/summary_spec.rb b/spec/models/quizzes/quiz_statistics/item_analysis/summary_spec.rb index 3c1cd93ec88..29524834a5c 100644 --- a/spec/models/quizzes/quiz_statistics/item_analysis/summary_spec.rb +++ b/spec/models/quizzes/quiz_statistics/item_analysis/summary_spec.rb @@ -48,7 +48,7 @@ describe Quizzes::QuizStatistics::ItemAnalysis::Summary do it "distributes the students accordingly" do buckets = summary.buckets - total = buckets.values.map(&:size).sum + total = buckets.values.sum(&:size) top, middle, bottom = buckets[:top].size / total.to_f, buckets[:middle].size / total.to_f, buckets[:bottom].size / total.to_f # because of the small sample size, this is slightly off, but close enough for gvt work diff --git a/spec/models/quizzes/quiz_user_messager_spec_helper.rb b/spec/models/quizzes/quiz_user_messager_spec_helper.rb index 734a22d7555..ae3b8f6d3f6 100644 --- a/spec/models/quizzes/quiz_user_messager_spec_helper.rb +++ b/spec/models/quizzes/quiz_user_messager_spec_helper.rb @@ -40,7 +40,7 @@ module Quizzes def recipient_messages(target_group) recipients = @finder.send("#{target_group}_students") - recipients.map(&:all_conversations).map(&:size).reduce(:+) || 0 + recipients.map(&:all_conversations).sum(&:size) end end end diff --git a/spec/selenium/grades/student_grades_page/gradebook_student_spec.rb b/spec/selenium/grades/student_grades_page/gradebook_student_spec.rb index 04087783564..59db7f4250c 100644 --- a/spec/selenium/grades/student_grades_page/gradebook_student_spec.rb +++ b/spec/selenium/grades/student_grades_page/gradebook_student_spec.rb @@ -108,7 +108,7 @@ describe 'Student Gradebook' do means = [] [0, 3, 6].each do |i| # the format below ensures that 18.0 is displayed as 18. - mean = format('%g' % (('%.2f' % (grades[i, 3].inject { |a, e| a + e }.to_f / 3)))) + mean = format('%g' % (('%.2f' % (grades[i, 3].sum.to_f / 3)))) means.push mean end