-
Notifications
You must be signed in to change notification settings - Fork 15
/
about_open_classes.rb
45 lines (36 loc) · 962 Bytes
/
about_open_classes.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
require File.expand_path(File.dirname(__FILE__) + '/edgecase')
class AboutOpenClasses < EdgeCase::Koan
class Dog
def bark
"WOOF"
end
end
def test_as_defined_dogs_do_bark
fido = Dog.new
assert_equal "WOOF", fido.bark
end
# ------------------------------------------------------------------
# Open the existing Dog class and add a new method.
class Dog
def wag
"HAPPY"
end
end
def test_after_reopening_dogs_can_both_wag_and_bark
fido = Dog.new
assert_equal "HAPPY", fido.wag
assert_equal "WOOF", fido.bark
end
# ------------------------------------------------------------------
class ::Integer
def even?
(self % 2) == 0
end
end
def test_even_existing_built_in_classes_can_be_reopened
assert_equal false, 1.even?
assert_equal true, 2.even?
end
# NOTE: To understand why we need the :: before Integer, you need to
# become enlightened about scope.
end