Class: Integer
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
some_task
end
40.percent_of_the_time(true, otherwise: false)
40.percent_of_the_time ? something : something_else
Instance Method Summary collapse
Instance Method Details
#percent_of_the_time(*arg, otherwise: nil) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/sometimes.rb', line 26 def percent_of_the_time(*arg, otherwise: nil) return if self == 0 raise(ArgumentError, 'Integer should be between 0 and 100 to be used with the percent_of_the_time method') if self < 0 || self > 100 if Kernel.rand(1..100) <= self if block_given? yield elsif arg.empty? true else arg.first end else format_otherwise(arg, otherwise) end end |