-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.rb
More file actions
62 lines (51 loc) · 1.06 KB
/
test.rb
File metadata and controls
62 lines (51 loc) · 1.06 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
class Proxy
def initialize(target_object)
@object = target_object
@message_log = {}
end
# WRITE CODE HERE
def method_missing(method_name, *args, &block)
if @object.respond_to?(method_name)
@message_log[method_name] = number_of_times_called(method_name) + 1
@object.send(method_name, *args, &block)
else
super(method_name, *args, &block)
end
end
def messages
@message_log.keys.reverse
end
def get_message_log
@message_log
end
def called?(method_name)
@message_log.key?(method_name)
end
def number_of_times_called(method_name)
@message_log[method_name] || 0
end
end
class Television
attr_accessor :channel, :power
def power
if @power == :on
@power = :off
else
@power = :on
end
end
def on?
@power == :on
end
end
mytv = Television.new
#p mytv.send(:channel=, 10)
#p mytv.send(:instance_variable_get, "@channel")
tv = Proxy.new(Television.new)
#p tv.inspect
#p tv.instance_of?(Proxy)
tv.channel = 10
#p tv.channel
tv.power
tv.power
p tv.get_message_log.keys