/
about_message_passing.rb
196 lines (154 loc) · 5.74 KB
/
about_message_passing.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
require File.expand_path(File.dirname(__FILE__) + '/neo')
class AboutMessagePassing < Neo::Koan
class MessageCatcher
def caught?
true
end
end
def test_methods_can_be_called_directly
mc = MessageCatcher.new
assert mc.caught?
end
def test_methods_can_be_invoked_by_sending_the_message
mc = MessageCatcher.new
assert mc.send(:caught?)
end
def test_methods_can_be_invoked_more_dynamically
mc = MessageCatcher.new
assert mc.send("caught?")
assert mc.send("caught" + '?' ) # What do you need to add to the first string?
assert mc.send("CAUGHT?".downcase ) # What would you need to do to the string?
end
def test_send_with_underscores_will_also_send_messages
mc = MessageCatcher.new
assert_equal true, mc.__send__(:caught?)
# THINK ABOUT IT:
#
# Why does Ruby provide both send and __send__ ?
#
# Answer: it's because some classes may want to define a method of their
# own called send, so to be safe, Ruby provides __send__.
end
def test_classes_can_be_asked_if_they_know_how_to_respond
mc = MessageCatcher.new
assert_equal true, mc.respond_to?(:caught?)
assert_equal false, mc.respond_to?(:does_not_exist)
end
# ------------------------------------------------------------------
class MessageCatcher
def add_a_payload(*args)
args
end
end
def test_sending_a_message_with_arguments
mc = MessageCatcher.new
# First thought was that this would return nil, but the splat operator
# gathers up any number of arguments into an array, so on second
# consideration, it makes sense that sending 0 args would == []
assert_equal [], mc.add_a_payload
assert_equal [], mc.send(:add_a_payload)
assert_equal [3, 4, nil, 6], mc.add_a_payload(3, 4, nil, 6)
assert_equal [3, 4, nil, 6], mc.send(:add_a_payload, 3, 4, nil, 6)
end
# ------------------------------------------------------------------
class TypicalObject
end
def test_sending_undefined_messages_to_a_typical_object_results_in_errors
typical = TypicalObject.new
exception = assert_raise(NoMethodError) do
typical.foobar
end
assert_match(/foobar/, exception.message)
end
def test_calling_method_missing_causes_the_no_method_error
typical = TypicalObject.new
exception = assert_raise(NoMethodError) do
typical.method_missing(:foobar)
end
assert_match(/foobar/, exception.message)
# THINK ABOUT IT:
#
# If the method :method_missing causes the NoMethodError, then
# what would happen if we redefine method_missing?
#
# NOTE:
#
# In Ruby 1.8 the method_missing method is public and can be
# called as shown above. However, in Ruby 1.9 (and later versions)
# the method_missing method is private. We explicitly made it
# public in the testing framework so this example works in both
# versions of Ruby. Just keep in mind you can't call
# method_missing like that after Ruby 1.9 normally.
#
# Thanks. We now return you to your regularly scheduled Ruby
# Koans.
#
# Answer: calling #method_missing causes a NoMethodError because it throws
# that exception explicitly if an object doesn't support that message. You
# can redefine #method_missing to have it intercept certain messages. For
# example, in one exercise for the ESaaS/CS169.1x textbook, we added an
# #hours method to Fixnum, and then we used :method_missing to have it
# redirect any calls using the singular (e.g. 1.hour). Of course, we still
# forward calls to super if we do not match...
end
# ------------------------------------------------------------------
class AllMessageCatcher
def method_missing(method_name, *args, &block)
"Someone called #{method_name} with <#{args.join(", ")}>"
end
end
def test_all_messages_are_caught
catcher = AllMessageCatcher.new
assert_equal 'Someone called foobar with <>', catcher.foobar
assert_equal 'Someone called foobaz with <1>', catcher.foobaz(1)
assert_equal 'Someone called sum with <1, 2, 3, 4, 5, 6>', catcher.sum(1,2,3,4,5,6)
end
# Whoa, unexpected! respond_to does not accept method_missing as a proper
# "response", and therefore any messages caught by method_missing without
# throwing an exception are not counted as responding to that message. Even
# when method_missing calls another method, it doesn't count.
def test_catching_messages_makes_respond_to_lie
catcher = AllMessageCatcher.new
assert_nothing_raised do
catcher.any_method
end
assert_equal false, catcher.respond_to?(:any_method)
end
# ------------------------------------------------------------------
class WellBehavedFooCatcher
def method_missing(method_name, *args, &block)
if method_name.to_s[0,3] == "foo"
"Foo to you too"
else
super(method_name, *args, &block)
end
end
end
def test_foo_method_are_caught
catcher = WellBehavedFooCatcher.new
assert_equal 'Foo to you too', catcher.foo_bar
assert_equal 'Foo to you too', catcher.foo_baz
end
def test_non_foo_messages_are_treated_normally
catcher = WellBehavedFooCatcher.new
assert_raise(NoMethodError) do
catcher.normal_undefined_method
end
end
# ------------------------------------------------------------------
# (note: just reopening class from above)
class WellBehavedFooCatcher
def respond_to?(method_name)
if method_name.to_s[0,3] == "foo"
true
else
super(method_name)
end
end
end
def test_explicitly_implementing_respond_to_lets_objects_tell_the_truth
catcher = WellBehavedFooCatcher.new
assert_equal true, catcher.respond_to?(:foo_bar)
assert_equal false, catcher.respond_to?(:something_else)
end
end