Class: ABTester::ChoiceOptions

Inherits:
Object
  • Object
show all
Defined in:
lib/ab_tester/choice_options.rb

Instance Method Summary collapse

Constructor Details

#initialize(options_hash) ⇒ ChoiceOptions

Returns a new instance of ChoiceOptions.



2
3
4
5
6
7
8
9
10
# File 'lib/ab_tester/choice_options.rb', line 2

def initialize(options_hash)
  @total_rank  = 0
  @ranked_keys = []

  options_hash.each do |key, value|
    @total_rank += value
    @ranked_keys << [key, @total_rank]
  end
end

Instance Method Details

#shuffleObject



12
13
14
15
# File 'lib/ab_tester/choice_options.rb', line 12

def shuffle
  random_value = rand @total_rank
  @ranked_keys.find{ |key, rank| random_value < rank }[0]
end