class VGen::ArrayGen

Public Class Methods

new( uniq: false, min: 4, max: 9, length: nil, size: nil, gens: [ proc {Random.new.rand} ] ) click to toggle source
# File lib/v_gen/array_gen.rb, line 3
def initialize(
      uniq: false,
      min: 4,
      max: 9,
      length: nil,
      size: nil,
      gens: [ proc {Random.new.rand} ]
    )
  @uniq = uniq
  @length = length || size || (min..max)
  @gens = gens
  @min = min
  @max = max
end

Public Instance Methods

call() click to toggle source
# File lib/v_gen/array_gen.rb, line 18
def call()
  used_values = []
  Array.new(array_length) {
    loop do
      candidate_value = @gens.sample.call
      if used_values.include?(candidate_value)
        next
      else
        used_values << candidate_value if @uniq
        break candidate_value
      end
    end
  }
end

Private Instance Methods

array_length() click to toggle source
# File lib/v_gen/array_gen.rb, line 35
def array_length
  length = Random.new.rand(@length) if @length.is_a? Range
  length = @length if @length.is_a? Integer
  raise "length (size) can't be negative" if length < 0
  length
end