-
Notifications
You must be signed in to change notification settings - Fork 0
/
the_solution.txt
56 lines (45 loc) · 1.27 KB
/
the_solution.txt
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
#
# First, declare a contract.
#
class ObjContract < DuckPond::Contract
has_method :to_s
has_method :length
end
#
# Then use it to check stuff!
#
ObjContract.fulfilled_by? "Hello"
=> true (or false if not fulfilled)
#
# The "bang" version will raise an error if the object isn't the duck.
#
ObjContract.fulfilled_by! "Hello"
=> true (or error if not fulfilled)
#
# So, going back to that example from the problem doc...
#
class Foo
def self.bar(obj)
ObjContract.fulfilled_by! obj
obj.foo!
fooinator = Test::Fooinator.new
@fooified_obj = fooinator.foo(obj)
end
end
#
# Viola!
#
# Final Tips:
#
# * Contracts should be commented liberally. They form the descripiton
# of a contract just as interfaces would in strongly typed languages.
# Developers will be prompted to consult the duck when they see a pair
# of binoculars in your code, so make sure they find what they're looking
# for.
#
# * Rather than writing the duckpond functionality directly within the methods
# themselves, you could easily put them in hooks or method aliases. If you do this
# though, remember that the point of this app is to make things *more* visible, not
# less, so don't go tucking these duck checks too far away from where they are needed
# or leave an obvious trail if you do!
#