Last active
April 8, 2017 21:02
This file contains 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
# 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