Last active
February 3, 2017 14:07
-
-
Save maicher/022355092563cedb4d1a91b333182fea to your computer and use it in GitHub Desktop.
Container pattern
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
module Container | |
# Initialized once (kind'a singleton) | |
def self.a | |
@a ||= Object.new | |
end | |
# Initialized every time. | |
def self.b | |
Object.new | |
end | |
# Initializd once a thread (kind'a threaded singleton) | |
def self.c | |
Thread.current[:c] ||= Object.new | |
end | |
end | |
Container.a.object_id #=> 70330567805800 | |
Container.a.object_id #=> 70330567805800 | |
Container.b.object_id #=> 70330563636160 | |
Container.b.object_id #=> 70330563591780 | |
Container.c.object_id #=> 70330567855620 | |
Thread.new { | |
Container.c.object_id #=> 70330563509440 | |
Container.c.object_id #=> 70330563509440 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment