Skip to content

Instantly share code, notes, and snippets.

@maxim maxim/singleton_or_not.rb
Last active Apr 8, 2017

Embed
What would you like to do?
# 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
You can’t perform that action at this time.