Module: Expgen::Randomizer
Instance Method Summary collapse
Instance Method Details
#randomize(tree) ⇒ Object
25 26 27 28 29 30 31 32 |
# File 'lib/expgen/randomizer.rb', line 25 def randomize(tree) case tree when Array then tree.map { |el| randomize(el) }.join when Nodes::Alternation then randomize(tree..sample) when Nodes::Group then repeat(tree.repeat, tree.max) { randomize(tree.elements) } when Nodes::Character then repeat(tree.repeat, tree.max) { tree.chars.sample } end end |
#range(min, max) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/expgen/randomizer.rb', line 5 def range(min, max) if min == "*" [0,5] elsif min == "+" [1,5] elsif min and max [min, max] elsif min [min, min] else [1,1] end end |
#repeat(min, max) ⇒ Object
19 20 21 22 23 |
# File 'lib/expgen/randomizer.rb', line 19 def repeat(min, max) first, last = range(min, max) number = rand(last - first + 1) + first number.times.map { yield }.join end |