Created
September 18, 2015 19:59
-
-
Save pezra/b09322ddca8350320e71 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This class takes a job, and maybe does it. | |
# Usage `Slacker.maybe(probability: 0.1) { something() } | |
class Slacker | |
# Probability should be a float between 0 and 1. | |
def self.maybe(probability:) | |
raise "Slacker needs a job to skip out on." unless block_given? | |
yield if rand() < probability | |
end | |
def self.often(&block) | |
maybe(probability: 0.5) { block.call } | |
end | |
def self.rarely(&block) | |
maybe(probability: 0.01) { block.call } | |
end | |
def self.one_in(i, &block) | |
maybe(probability: 1.0 / i.to_f) { block.call } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment