Sha256: 1a61e990ab6828309e762dd23add5b22b27f31d7d770871e9855cf4eaae60c5c
Contents?: true
Size: 584 Bytes
Versions: 22
Compression:
Stored size: 584 Bytes
Contents
# frozen_string_literal: true Array.class_eval do # Sums the array with each element weighing 10 times as the next. # Expects `self` to be [Array<Numeric>] # # @raise [RubyRailsExtensions::Errors::NonNumericError] if any element is not Numeric # # @return [Integer] # def weighted_sum each_with_index.sum do |val, index| unless Numeric === val raise( RubyRailsExtensions::Errors::NonNumericError, "Wrong element type (given #{val.class}, expected Numeric)" ) end val * (10**(size - index)) end end end
Version data entries
22 entries across 22 versions & 1 rubygems