Skip to content

Instantly share code, notes, and snippets.

@maxim
Last active April 8, 2017 21:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxim/04a133f4fb576f1d34bc9391eb0b61a7 to your computer and use it in GitHub Desktop.
Save maxim/04a133f4fb576f1d34bc9391eb0b61a7 to your computer and use it in GitHub Desktop.
# You could have an object for the whole app to use (e.g. in rails initializer)
ChanceGenerator = Chance.new
# But also easily testable (this would be in a test setup):
test_chance = Chance.new(seed: 1)
# The constructor can have a little documentation:
class Chance
# If seed is omitted, a new seed will be generated for each random call
def initialize(seed: nil)
# …
end
# …
end
# This takes care of making class testable, and has good hygiene of working with randomness:
# a random class that accepts a seed is only natural.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment