forked from opal/opal
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingleton.rb
More file actions
39 lines (34 loc) · 776 Bytes
/
singleton.rb
File metadata and controls
39 lines (34 loc) · 776 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
module Singleton
def clone
raise TypeError, "can't clone instance of singleton #{self.class}"
end
def dup
raise TypeError, "can't dup instance of singleton #{self.class}"
end
module SingletonClassMethods
def clone
Singleton.__init__(super)
end
def inherited(sub_klass)
super
Singleton.__init__(sub_klass)
end
end
class << Singleton
def __init__(klass)
klass.instance_eval do
@singleton__instance__ = nil
end
def klass.instance
return @singleton__instance__ if @singleton__instance__
@singleton__instance__ = new
end
klass
end
def included(klass)
super
klass.extend SingletonClassMethods
Singleton.__init__(klass)
end
end
end