Skip to content

Instantly share code, notes, and snippets.

@maicher
Last active February 3, 2017 14:07
Show Gist options
  • Save maicher/022355092563cedb4d1a91b333182fea to your computer and use it in GitHub Desktop.
Save maicher/022355092563cedb4d1a91b333182fea to your computer and use it in GitHub Desktop.
Container pattern
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