Created
February 18, 2016 18:49
-
-
Save dsci/7891de5011a98eff6502 to your computer and use it in GitHub Desktop.
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
class SingletonCvar | |
def self.instance | |
@@instance ||= new | |
end | |
private_class_method :new | |
end | |
singleton = SingletonCvar.instance | |
singleton_again = SingletonCvar.instance | |
p singleton.object_id # => 70235617244200 | |
p singleton_again.object_id # => 70235617244200 | |
SingletonCvar.new | |
# singleton_cvar.rb:15:in `<main>': private method `new' called | |
# for SingletonCvar:Class (NoMethodError) |
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
module Singleton | |
def self.included(base) | |
base.class_eval do | |
def self.instance | |
@instance ||= new | |
end | |
private_class_method :new | |
end | |
end | |
end | |
class SingletonIvar | |
include Singleton | |
end | |
singletion = SingletonIvar.instance | |
p singletion.object_id # => 70242076750240 | |
singleton_again = SingletonIvar.instance | |
p singleton_again.object_id # => 70242076750240 | |
SingletonIvar.new | |
# singleton_ivar.rb:21:in `<main>': private method `new' | |
# called for SingletonIvar:Class (NoMethodError) |
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
# No usage of private_class_method here | |
module Singleton | |
def self.included(base) | |
base.send(:extend, ClassMethods) | |
end | |
module ClassMethods | |
def instance | |
singleton_class.send(:private, :new) unless is_private_method?(:new) | |
@instance ||= new | |
end | |
private | |
def is_private_method?(meth_name) | |
self.private_methods.include?(meth_name) | |
end | |
end | |
end | |
class SingletonIvarWoPrivateClassMethods | |
include Singleton | |
def foo | |
12 | |
end | |
end | |
singletion = SingletonIvarWoPrivateClassMethods.instance | |
p singletion.object_id #=> 70174947355600 | |
singleton_again = SingletonIvarWoPrivateClassMethods.instance | |
p singleton_again.object_id #=> 70174947355600 | |
SingletonIvarWoPrivateClassMethods.new | |
#=> singleton_wo_private_class_method.rb:35:in `<main>': private method `new' | |
# called for SingletonIvarWoPrivateClassMethods:Class (NoMethodError) |
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
require 'singleton' # use Ruby implementation | |
class SingletonImp | |
include Singleton | |
end | |
singletion = SingletonImp.instance | |
p singletion.object_id #=> 70127047740700 | |
singleton_again = SingletonImp.instance | |
p singleton_again.object_id #=> 70127047740700 | |
SingletonImp.new |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment