Module: Quick::Sampler::DSL::SimpleValues
- Included in:
- Quick::Sampler::DSL
- Defined in:
- lib/quick/sampler/dsl/simple_values.rb
Overview
Samplers of simple values to form the basis of the sampled data structure.
Note from the future
In the future simple values are sampled from other excellent Gems from behind a composable Quick Sampler API. In the mean time this is possible at the cost of readablity:
Instance Method Summary collapse
-
#boolean ⇒ Quick::Sampler<Boolean>
A sampler of
true
andfalse
values. -
#const(const) ⇒ Quick::Sampler<Anything>
Degenerate constant sampler.
-
#fixnum ⇒ Quick::Sampler<Fixnum>
Samples random fixnums (smaller integers that can be handled quickly by the CPU itself).
-
#integer ⇒ Quick::Sampler<Fixnum>
A sampler of integers prefering smaller ones.
-
#negative_fixnum ⇒ Quick::Sampler<Fixnum>
(also: #negative_integer)
A sampler of negative fixnums.
-
#positive_fixnum ⇒ Quick::Sampler<Fixnum>
(also: #positive_integer)
A sampler of positive fixnums.
-
#probability ⇒ Quick::Sampler<Float>
"Probability" sampler.
-
#string(*classes, size: 1..10) ⇒ Object
The sampler will produce strings of whose length is controlled by
size:
argument made up of characters belonging to supplied named classes. -
#weighted_truth(weight) ⇒ Quick::Sampler<Boolean>
Weighted truth sampler.
-
#zero ⇒ Quick::Sampler<0>
Degenerate sampler of zeros.
Instance Method Details
#boolean ⇒ Quick::Sampler<Boolean>
Returns a sampler of true
and false
values.
74 75 76 |
# File 'lib/quick/sampler/dsl/simple_values.rb', line 74 def boolean pick_from([true, false]) end |
#const(const) ⇒ Quick::Sampler<Anything>
Degenerate constant sampler. Will probably be superseeded by a cleaner smarter syntax as I get a better hang of it.
24 25 26 |
# File 'lib/quick/sampler/dsl/simple_values.rb', line 24 def const const feed { const } end |
#fixnum ⇒ Quick::Sampler<Fixnum>
Samples random fixnums (smaller integers that can be handled quickly by the CPU itself)
43 44 45 |
# File 'lib/quick/sampler/dsl/simple_values.rb', line 43 def fixnum pick_from(FixnumRange) end |
#integer ⇒ Quick::Sampler<Fixnum>
A sampler of integers prefering smaller ones
It will however sample a large one (from the Fixnum range) occasionally.
62 63 64 65 66 67 68 |
# File 'lib/quick/sampler/dsl/simple_values.rb', line 62 def integer one_of_weighted(fixnum => 5, pick_from(-1_000_000_000..1_000_000_000) => 7, pick_from(-1000..1000) => 9, pick_from(-100..100) => 11, pick_from(-20..20) => 17) end |
#negative_fixnum ⇒ Quick::Sampler<Fixnum> Also known as: negative_integer
Returns a sampler of negative fixnums.
48 49 50 |
# File 'lib/quick/sampler/dsl/simple_values.rb', line 48 def negative_fixnum pick_from(FixnumRange.min..-1) end |
#positive_fixnum ⇒ Quick::Sampler<Fixnum> Also known as: positive_integer
Returns a sampler of positive fixnums.
53 54 55 |
# File 'lib/quick/sampler/dsl/simple_values.rb', line 53 def positive_fixnum pick_from(1..FixnumRange.max) end |
#probability ⇒ Quick::Sampler<Float>
"Probability" sampler
99 100 101 |
# File 'lib/quick/sampler/dsl/simple_values.rb', line 99 def probability pick_from(0..1.0) end |
#string(*classes, size: 1..10) ⇒ Object
document character classes
The sampler will produce strings of whose length is controlled by
size:
argument made up of characters belonging to supplied named
classes.
88 89 90 91 92 93 |
# File 'lib/quick/sampler/dsl/simple_values.rb', line 88 def string *classes, size: 1..10 classes = [:printable] if classes.empty? repertoire = DSL::CharacterClass.(*classes) size = pick_from(size) if Range === size send_to( send_to(repertoire, :sample, size), :join ) end |
#weighted_truth(weight) ⇒ Quick::Sampler<Boolean>
Weighted truth sampler
109 110 111 |
# File 'lib/quick/sampler/dsl/simple_values.rb', line 109 def weighted_truth weight one_of_weighted true => weight, false => (1 - weight) end |
#zero ⇒ Quick::Sampler<0>
Degenerate sampler of zeros. Like #const this will probably go away soon.
32 33 34 |
# File 'lib/quick/sampler/dsl/simple_values.rb', line 32 def zero const 0 end |