bef7effde9422b0c39ba4681cf7bd544475d97b1
[ruby_koans.git] / src / about_open_classes.rb
1 require File.expand_path(File.dirname(__FILE__) + '/edgecase')
2
3 class AboutOpenClasses < EdgeCase::Koan
4   class Dog
5     def bark
6       "WOOF"
7     end
8   end
9
10   def test_as_defined_dogs_do_bark
11     fido = Dog.new
12     assert_equal __("WOOF"), fido.bark
13   end
14
15   # ------------------------------------------------------------------
16
17   # Open the existing Dog class and add a new method.
18   class Dog
19     def wag
20       "HAPPY"
21     end
22   end
23
24   def test_after_reopening_dogs_can_both_wag_and_bark
25     fido = Dog.new
26     assert_equal __("HAPPY"), fido.wag
27     assert_equal __("WOOF"), fido.bark
28   end
29
30   # ------------------------------------------------------------------
31
32   class ::Integer
33     def even?
34       (self % 2) == 0
35     end
36   end
37
38   def test_even_existing_built_in_classes_can_be_reopened
39     assert_equal __(false), 1.even?
40     assert_equal __(true), 2.even?
41   end
42
43   # NOTE: To understand why we need the :: before Integer, you need to
44   # become enlightened about scope.  
45 end