Class: RuleChooser
- Inherits:
-
Object
- Object
- RuleChooser
- Defined in:
- lib/grammoire/rule_chooser.rb
Instance Method Summary collapse
-
#initialize(random_generator = RandomGenerator.new) ⇒ RuleChooser
constructor
A new instance of RuleChooser.
- #select_from(rules) ⇒ Object
Constructor Details
#initialize(random_generator = RandomGenerator.new) ⇒ RuleChooser
Returns a new instance of RuleChooser.
3 4 5 |
# File 'lib/grammoire/rule_chooser.rb', line 3 def initialize(random_generator = RandomGenerator.new) @random_generator = random_generator end |
Instance Method Details
#select_from(rules) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/grammoire/rule_chooser.rb', line 7 def select_from(rules) return nil if rules.empty? selected_weight_index = @random_generator.number_up_to(total_weight_from(rules)) max_weight_index = 0 rules.each do |rule| max_weight_index += rule.weight return rule if selected_weight_index < max_weight_index end end |