Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

40 lines (35 sloc) 0.691 kb
# OH SO FUN HELPERS!
# 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
class Fixnum
def percent_of_the_time(&block)
raise(ArgumentError, 'Fixnum should be between 1 and 100 to be used with the times method') unless self > 0 && self <= 100
yield block if (Kernel.rand(99)+1) <= self
end
end
# (3..6).times do
class Range
def times(&block)
self.to_a.rand.times(&block)
end
end
# half_the_time do
# sometimes do
class Object
def half_the_time(&block)
50.percent_of_the_time {yield}
end
alias :sometimes :half_the_time
end
Jump to Line
Something went wrong with that request. Please try again.