Class: VGen::HashGen

Inherits:
Object
  • Object
show all
Defined in:
lib/v_gen/hash_gen.rb,
lib/v_gen/old_hash_gen.rb

Instance Method Summary collapse

Constructor Details

#initialize(max_depth: 3) ⇒ HashGen

Returns a new instance of HashGen.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/v_gen/hash_gen.rb', line 3

def initialize(
      with: {},
      min: 4,
      max: 8,
      length: nil,
      size: nil,
      key_gens:   [ proc {Random.new.rand(0..100)} ],
      value_gens: [ proc {Random.new.rand} ]
    )
  @with = with
  @length = length || size || (min..max)
  @key_gens = key_gens
  @value_gens = value_gens
  @min = min
  @max = max
end

Instance Method Details

#call(only: [ IntGen.new, FloatGen.new, VarWordGen.new, ArrayGen.new, self.class.new, KeywordGen.new ], except: [], min: 4, max: 8) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/v_gen/hash_gen.rb', line 20

def call()
  with_length = @with.length
  hash = Hash[
    Array.new(hash_length - with_length) do
      [
        @key_gens.sample.call,
        @value_gens.sample.call
      ]
    end
  ]
  unless @with.empty?
    hash.merge!(@with)
  end
  while hash.size < hash_length do
    new_pair =  {
      @key_gens.sample.call => @value_gens.sample.call
    }
    hash = new_pair.merge(hash)  
  end
  hash = hash.to_a.shuffle.to_h unless @with.empty?
  hash
end