Class: RouletteWheelSelection
- Inherits:
-
Object
- Object
- RouletteWheelSelection
- Defined in:
- lib/roulette-wheel-selection.rb,
lib/roulette-wheel-selection/version.rb
Constant Summary collapse
- NOT_SET =
Object.new
- VERSION =
'1.1.1'
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(hash) ⇒ RouletteWheelSelection
constructor
A new instance of RouletteWheelSelection.
- #sample(num = NOT_SET) ⇒ Object
Constructor Details
#initialize(hash) ⇒ RouletteWheelSelection
Returns a new instance of RouletteWheelSelection.
30 31 32 33 |
# File 'lib/roulette-wheel-selection.rb', line 30 def initialize(hash) @hash = hash @total_rate = hash.values.inject(0, :+) end |
Class Method Details
.sample(*args) ⇒ Object
9 10 11 12 13 14 15 16 |
# File 'lib/roulette-wheel-selection.rb', line 9 def sample(*args) object = args.first case object when Array ; sample_from_array(*args) when Hash ; sample_from_hash(*args) else ; fail "Unsupported type: #{object.class}" end end |
Instance Method Details
#sample(num = NOT_SET) ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/roulette-wheel-selection.rb', line 35 def sample(num = NOT_SET) return if @total_rate == 0 return sample_an_object(@total_rate, @hash) if num == NOT_SET return if num < 1 return sample_n_objects(num) if num > 1 return [sample_an_object(@total_rate, @hash)] end |