Fix some typos.
[ruby_koans.git] / koans / about_constants.rb
1 require File.expand_path(File.dirname(__FILE__) + '/edgecase')
2
3 C = "top level"
4
5 class AboutConstants < EdgeCase::Koan
6
7   C = "nested"
8
9   def test_nested_constants_may_also_be_referenced_with_relative_paths
10     assert_equal __, C
11   end
12
13   def test_top_level_constants_are_referenced_by_double_colons
14     assert_equal __, ::C
15   end
16
17   def test_nested_constants_are_referenced_by_their_complete_path
18     assert_equal __, AboutConstants::C
19     assert_equal __, ::AboutConstants::C
20   end
21
22   # ------------------------------------------------------------------
23
24   class Animal
25     LEGS = 4
26     def legs_in_animal
27       LEGS
28     end
29
30     class NestedAnimal
31       def legs_in_nested_animal
32         LEGS
33       end
34     end
35   end
36
37   def test_nested_classes_inherit_constants_from_enclosing_classes
38     assert_equal __, Animal::NestedAnimal.new.legs_in_nested_animal
39   end
40
41   # ------------------------------------------------------------------
42
43   class Reptile < Animal
44     def legs_in_reptile
45       LEGS
46     end
47   end
48
49   def test_subclasses_inherit_constants_from_parent_classes
50     assert_equal __, Reptile.new.legs_in_reptile
51   end
52
53   # ------------------------------------------------------------------
54
55   class MyAnimals
56     LEGS = 2
57
58     class Bird < Animal
59       def legs_in_bird
60         LEGS
61       end
62     end
63   end
64
65   def test_who_wins_with_both_nested_and_inherited_constants
66     assert_equal __, MyAnimals::Bird.new.legs_in_bird
67   end
68
69   # QUESTION: Which has precedence: The constant in the lexical scope,
70   # or the constant from the inheritance hierarchy?
71
72   # ------------------------------------------------------------------
73
74   class MyAnimals::Oyster < Animal
75     def legs_in_oyster
76       LEGS
77     end
78   end
79
80   def test_who_wins_with_explicit_scoping_on_class_definition
81     assert_equal __, MyAnimals::Oyster.new.legs_in_oyster
82   end
83
84   # QUESTION: Now which has precedence: The constant in the lexical
85   # scope, or the constant from the inheritance hierarchy?  Why is it
86   # different than the previous answer?
87 end