Class: Properb::Generators::ChoiceGenerator
- Inherits:
-
Properb::Generator
- Object
- Properb::Generator
- Properb::Generators::ChoiceGenerator
- Defined in:
- lib/properb/generators/choice_generator.rb
Instance Method Summary collapse
- #generate_value(random, size) ⇒ Object
-
#initialize(generators) ⇒ ChoiceGenerator
constructor
A new instance of ChoiceGenerator.
-
#or(generator) ⇒ Object
Overriding the superclass method to make the distribution even.
Methods inherited from Properb::Generator
#map, #reject, #select, #sized, #to_properb_generator
Constructor Details
#initialize(generators) ⇒ ChoiceGenerator
4 5 6 |
# File 'lib/properb/generators/choice_generator.rb', line 4 def initialize(generators) @generators = generators end |
Instance Method Details
#generate_value(random, size) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/properb/generators/choice_generator.rb', line 13 def generate_value(random, size) integer = IntegerGenerator.new(0...@generators.length) .generate_value(random, size) saved_random = random.dup integer.flat_map do |choice| # We want all the sub-generators use the same random number # sequence, so we duplicate the generator new_random = saved_random.dup @generators[choice].generate_value(new_random, size) end end |
#or(generator) ⇒ Object
Overriding the superclass method to make the distribution even
9 10 11 |
# File 'lib/properb/generators/choice_generator.rb', line 9 def or(generator) ChoiceGenerator.new([*@generators, generator]) end |