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

Raises:

  • (ArgumentError)


21
22
23
24
# File 'lib/sometimes.rb', line 21

def percent_of_the_time(&block)
  raise(ArgumentError, 'Integer should be between 1 and 100 to be used with the times method') unless self > 0 && self <= 100
  yield if (Kernel.rand(99)+1) <= self
end