Class: WeightedList::Normalizer

Inherits:
Object
  • Object
show all
Defined in:
lib/weighted_list/normalizer.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection) ⇒ Normalizer

Returns a new instance of Normalizer.



9
10
11
# File 'lib/weighted_list/normalizer.rb', line 9

def initialize(collection)
  @hash = collection.to_h
end

Class Method Details

.call(collection) ⇒ Object



5
6
7
# File 'lib/weighted_list/normalizer.rb', line 5

def self.call(collection)
  new(collection).call
end

Instance Method Details

#callObject



13
14
15
16
17
# 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