Sha256: 91e5135a5ef3f0ecfc897e2858e53b1a5802390bae1dbce5fb691da9aeddd0f1

Contents?: true

Size: 961 Bytes

Versions: 7

Compression:

Stored size: 961 Bytes

Contents

module VGen
  class ArrayGen
    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

    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

    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
  end
end

Version data entries

7 entries across 5 versions & 2 rubygems

Version Path
bsielski_control_flow-1.0.0 vendor/bundle/ruby/2.5.0/gems/bsielski_value_generator-1.2.0/lib/bsielski_v_gen/array_gen.rb
bsielski_control_flow-1.0.0 vendor/bundle/ruby/2.5.0/gems/bsielski_value_generator-1.4.0/lib/v_gen/array_gen.rb
bsielski_control_flow-1.0.0 vendor/bundle/ruby/2.5.0/gems/bsielski_value_generator-1.3.0/lib/v_gen/array_gen.rb
bsielski_value_generator-1.4.0 lib/v_gen/array_gen.rb
bsielski_value_generator-1.3.0 lib/v_gen/array_gen.rb
bsielski_value_generator-1.2.0 lib/bsielski_v_gen/array_gen.rb
bsielski_value_generator-1.1.0 lib/bsielski_v_gen/array_gen.rb