-
Notifications
You must be signed in to change notification settings - Fork 0
/
singleton.rb
46 lines (37 loc) · 1.13 KB
/
singleton.rb
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
40
41
42
43
44
45
46
class Singleton
def self.allocate
@instance ||= super
end
def clone
@instance ||= super
end
def dup
@instance ||= super
end
def self.new
@instance ||= super
end
def self.singleton_method_added(method)
@singleton_new_trial ||= (0..1).cycle
if method.to_sym == :new || method.to_sym == :allocate
method_symbol = method.to_sym
self.define_singleton_method(method_symbol, Singleton.singleton_method(method_symbol)) if @singleton_new_trial.next == 0
raise TypeError, "You can't define a singleton #{method_symbol} method while inheriting from Singleton"
end
end
def self.method_added(method)
@instance_clone_trial ||= (0..1).cycle
puts "method name is #{method}"
if method.to_sym == :clone || method.to_sym == :dup
method_symbol = method.to_sym
self.define_method(method_symbol, Singleton.method(method_symbol)) if @instance_clone_trial.next == 0
raise TypeError, "You can't define a singleton #{method_symbol} method while inheriting from Singleton"
end
end
def self.instance
@instance
end
def self.instance?
@instance != nil
end
end