class HashMap def initialize @prizes = [35, 25, 20, 15, 5] @participations = [ Participation.new(10), Participation.new(10), Participation.new(10), Participation.new(6), Participation.new(4), Participation.new(4) ] @percent = 10 end def mapping # try participations into groups directly without this first array of hashes and then create that... raw_distribution = @participations.map.with_index do |current, i| prize = @prizes[i] ? @prizes[i] * @percent : 0 prize_percent = @prizes[i] ? @prizes[i] : 0 { position: i+1, participation: current, percentage: prize_percent, prize: prize } end puts '========> before <==========' puts raw_distribution groups = raw_distribution.group_by {|pos| pos[:participation].score } distribution = [] groups.each do |group| recalculated_percentage = 0 group.last.map{ |part| recalculated_percentage += part[:percentage]} recalculated_percentage = recalculated_percentage.to_f / group.last.count group.last.map do |part| part[:percentage] = recalculated_percentage part[:prize] = recalculated_percentage * @percent distribution << part end end puts '========> after <==========' puts distribution end end class Participation attr_accessor :score def initialize score @score = score end end h = HashMap.new h.mapping