Class: Integer

Inherits:
Object show all
Defined in:
lib/sometimes.rb

Overview

RANDOMLY EXECUTES A BLOCK X percent OF THE TIME

TEST WITH

i = 0 100000.times do

75.percent_of_the_time do
  i += 1
end

end i

40.percent_of_the_time do

Instance Method Summary collapse

Instance Method Details

#percent_of_the_time(&block) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/sometimes.rb', line 21

def percent_of_the_time(&block)
  return if self == 0

  if self < 0 || self > 100
    raise(ArgumentError, 'Integer should be between 0 and 100 to be used with the percent_of_the_time method')
  else
    yield block if Kernel.rand(1..100) <= self
  end
end