class WeightedList::Normalizer

Attributes

hash[R]

Public Class Methods

call(collection) click to toggle source
# File lib/weighted_list/normalizer.rb, line 5
def self.call(collection)
  new(collection).call
end
new(collection) click to toggle source
# File lib/weighted_list/normalizer.rb, line 9
def initialize(collection)
  @hash = collection.to_h
end

Public Instance Methods

call() click to toggle source
# File lib/weighted_list/normalizer.rb, line 13
def call
  hash.each_with_object({}) do |(item, weight), normalized_hash|
    normalized_hash[item] = weight.fdiv(total_weight)
  end
end

Private Instance Methods

total_weight() click to toggle source
# File lib/weighted_list/normalizer.rb, line 23
def total_weight
  @total_weight ||= hash.values.reduce(&:+)
end