class Rantly

Constants

INTEGER_MAX

wanna avoid going into Bignum when calling range with these.

INTEGER_MIN

Attributes

default_size[W]
classifiers[RW]

Public Class Methods

default_size() click to toggle source
# File lib/rantly/generator.rb, line 9
def default_size
  @default_size || 6
end
each(n, limit = 10, &block) click to toggle source
# File lib/rantly/generator.rb, line 13
def each(n, limit = 10, &block)
  gen.each(n, limit, &block)
end
gen() click to toggle source
# File lib/rantly/generator.rb, line 25
def gen
  singleton
end
map(n, limit = 10, &block) click to toggle source
# File lib/rantly/generator.rb, line 17
def map(n, limit = 10, &block)
  gen.map(n, limit, &block)
end
new() click to toggle source
# File lib/rantly/generator.rb, line 87
def initialize
  reset
end
singleton() click to toggle source
# File lib/rantly/generator.rb, line 4
def singleton
  @singleton ||= Rantly.new
  @singleton
end
value(limit = 10, &block) click to toggle source
# File lib/rantly/generator.rb, line 21
def value(limit = 10, &block)
  gen.value(limit, &block)
end

Public Instance Methods

array(n = size, &block) click to toggle source
# File lib/rantly/generator.rb, line 219
def array(n = size, &block)
  n.times.map { instance_eval(&block) }
end
boolean() click to toggle source
# File lib/rantly/generator.rb, line 190
def boolean
  range(0, 1).zero?
end
branch(*gens) click to toggle source
# File lib/rantly/generator.rb, line 178
def branch(*gens)
  call(choose(*gens))
end
call(gen, *args) click to toggle source
# File lib/rantly/generator.rb, line 163
def call(gen, *args)
  case gen
  when Symbol
    send(gen, *args)
  when Array
    raise 'empty array' if gen.empty?

    send(gen[0], *gen[1..-1])
  when Proc
    instance_eval(&gen)
  else
    raise "don't know how to call type: #{gen}"
  end
end
choose(*vals) click to toggle source
# File lib/rantly/generator.rb, line 182
def choose(*vals)
  vals[range(0, vals.length - 1)] if vals.length.positive?
end
classify(classifier) click to toggle source
# File lib/rantly/generator.rb, line 96
def classify(classifier)
  @classifiers[classifier] += 1
end
dict(n = size, &block) click to toggle source
# File lib/rantly/generator.rb, line 223
def dict(n = size, &block)
  h = {}
  each(n) do
    k, v = instance_eval(&block)
    h[k] = v if guard(!h.key?(k))
  end
  h
end
each(n, limit = 10, &block) click to toggle source

limit attempts to 10 times of how many things we want to generate

# File lib/rantly/generator.rb, line 47
def each(n, limit = 10, &block)
  generate(n, limit, block)
end
float(distribution = nil, params = {}) click to toggle source
# File lib/rantly/generator.rb, line 144
def float(distribution = nil, params = {})
  case distribution
  when :normal
    params[:center] ||= 0
    params[:scale] ||= 1
    raise 'The distribution scale should be greater than zero' if params[:scale].negative?

    # Sum of 6 draws from a uniform distribution give as a draw of a normal
    # distribution centered in 3 (central limit theorem).
    ([rand, rand, rand, rand, rand, rand].sum - 3) * params[:scale] + params[:center]
  else
    rand
  end
end
freq(*pairs) click to toggle source
# File lib/rantly/generator.rb, line 194
def freq(*pairs)
  pairs = pairs.map do |pair|
    case pair
    when Symbol, String, Proc
      [1, pair]
    when Array
      if pair.first.is_a?(Integer)
        pair
      else
        [1] + pair
      end
    end
  end
  total = pairs.inject(0) { |sum, p| sum + p.first }
  raise("Illegal frequency:#{pairs.inspect}") if total.zero?

  pos = range(1, total)
  pairs.each do |p|
    weight, gen, *args = p
    return call(gen, *args) if pos <= p[0]

    pos -= weight
  end
end
generate(n, limit_arg, gen_block) { |val| ... } click to toggle source
# File lib/rantly/generator.rb, line 65
def generate(n, limit_arg, gen_block, &handler)
  limit = n * limit_arg
  nfailed = 0
  nsuccess = 0
  while nsuccess < n
    raise TooManyTries.new(limit_arg * n, nfailed) if limit.zero?

    begin
      val = instance_eval(&gen_block)
    rescue GuardFailure
      nfailed += 1
      limit -= 1
      next
    end
    nsuccess += 1
    limit -= 1
    yield(val) if handler
  end
end
guard(test) click to toggle source
# File lib/rantly/generator.rb, line 100
def guard(test)
  return true if test

  raise GuardFailure
end
integer(limit = nil) click to toggle source
# File lib/rantly/generator.rb, line 123
def integer(limit = nil)
  case limit
  when Range
    hi = limit.end
    lo = limit.begin
  when Integer
    raise 'n should be greater than zero' if limit.negative?

    hi = limit
    lo = -limit
  else
    hi = INTEGER_MAX
    lo = INTEGER_MIN
  end
  range(lo, hi)
end
literal(value) click to toggle source
# File lib/rantly/generator.rb, line 186
def literal(value)
  value
end
map(n, limit = 10, &block) click to toggle source
# File lib/rantly/generator.rb, line 51
def map(n, limit = 10, &block)
  acc = []
  generate(n, limit, block) do |val|
    acc << val
  end
  acc
end
positive_integer() click to toggle source
# File lib/rantly/generator.rb, line 140
def positive_integer
  range(0)
end
range(lo = INTEGER_MIN, hi = INTEGER_MAX) click to toggle source
# File lib/rantly/generator.rb, line 159
def range(lo = INTEGER_MIN, hi = INTEGER_MAX)
  rand(lo..hi)
end
reset() click to toggle source
# File lib/rantly/generator.rb, line 91
def reset
  @size = nil
  @classifiers = Hash.new(0)
end
size() click to toggle source
# File lib/rantly/generator.rb, line 106
def size
  @size || Rantly.default_size
end
sized(n, &block) click to toggle source
# File lib/rantly/generator.rb, line 110
def sized(n, &block)
  raise 'size needs to be greater than zero' if n.negative?

  old_size = @size
  @size = n
  r = instance_eval(&block)
  @size = old_size
  r
end
string(char_class = :print) click to toggle source
# File lib/rantly/generator.rb, line 272
def string(char_class = :print)
  chars = case char_class
          when Regexp
            Chars.of(char_class)
          when Symbol
            Chars::CLASSES[char_class]
          end
  raise 'bad arg' unless chars

  char_strings = chars.map(&:chr)
  str = Array.new(size)
  size.times { |i| str[i] = char_strings.sample }
  str.join
end
value(limit = 10, &block) click to toggle source
# File lib/rantly/generator.rb, line 59
def value(limit = 10, &block)
  generate(1, limit, block) do |val|
    return val
  end
end