Class: Randrizer::Types::Nullable

Inherits:
BaseType
  • Object
show all
Defined in:
lib/randrizer/types/nullable.rb

Instance Method Summary collapse

Constructor Details

#initialize(null_prob:, inner_type:) ⇒ Nullable

Returns a new instance of Nullable.



8
9
10
11
# File 'lib/randrizer/types/nullable.rb', line 8

def initialize(null_prob:, inner_type:)
  @null_prob = null_prob
  @inner_type = inner_type
end

Instance Method Details

#evalObject



18
19
20
21
22
# File 'lib/randrizer/types/nullable.rb', line 18

def eval
  return nil if rand > (1.0 - @null_prob)

  @inner_type.eval
end

#validate!Object



13
14
15
16
# File 'lib/randrizer/types/nullable.rb', line 13

def validate!
  raise ValidationError("null_prob must be < 1.0") if @null_prob > 1.0
  raise ValidationError("null_prob must be > 0.0") if @null_prob < 0.0
end