Skip to content

Instantly share code, notes, and snippets.

@pezra
Created September 18, 2015 19:59
Show Gist options
  • Save pezra/b09322ddca8350320e71 to your computer and use it in GitHub Desktop.
Save pezra/b09322ddca8350320e71 to your computer and use it in GitHub Desktop.
# 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