Added about_constants
authorJim Weirich <jim.weirich@gmail.com>
Tue, 20 Apr 2010 20:17:34 +0000 (16:17 -0400)
committerJim Weirich <jim.weirich@gmail.com>
Tue, 20 Apr 2010 20:17:34 +0000 (16:17 -0400)
koans/about_constants.rb [new file with mode: 0644]
src/about_constants.rb [new file with mode: 0644]
src/path_to_enlightenment.rb

diff --git a/koans/about_constants.rb b/koans/about_constants.rb
new file mode 100644 (file)
index 0000000..ae270ed
--- /dev/null
@@ -0,0 +1,87 @@
+require 'edgecase'
+
+C = "top level"
+
+class AboutConstants < EdgeCase::Koan
+
+  C = "nested"
+
+  def test_nested_constants_may_also_be_referenced_with_relative_paths
+    assert_equal __, C
+  end
+
+  def test_top_level_constants_are_referenced_by_double_colons
+    assert_equal __, ::C
+  end
+
+  def test_nested_constants_are_referenced_by_their_complete_path
+    assert_equal __, AboutConstants::C
+    assert_equal __, ::AboutConstants::C
+  end
+
+  # ------------------------------------------------------------------
+  
+  class Animal
+    LEGS = 4
+    def legs_in_animal
+      LEGS
+    end
+
+    class NestedAnimal
+      def legs_in_nested_animal
+        LEGS
+      end
+    end
+  end
+  
+  def test_nested_classes_inherit_constants_from_enclosing_classes
+    assert_equal __, Animal::NestedAnimal.new.legs_in_nested_animal    
+  end
+
+  # ------------------------------------------------------------------
+  
+  class Reptile < Animal
+    def legs_in_reptile
+      LEGS
+    end
+  end
+  
+  def test_subclasses_inherit_constants_from_parent_classes
+    assert_equal __, Reptile.new.legs_in_reptile
+  end
+  
+  # ------------------------------------------------------------------
+  
+  class MyAnimals
+    LEGS = 2
+
+    class Bird < Animal
+      def legs_in_bird
+        LEGS
+      end
+    end
+  end
+  
+  def test_who_wins_with_both_nested_and_inherited_constants
+    assert_equal __, MyAnimals::Bird.new.legs_in_bird
+  end
+  
+  # QUESTION: Which has precedence: The constant in the lexical scope,
+  # or the constant from the inheritance heirarachy?
+  
+  # ------------------------------------------------------------------
+  
+  class MyAnimals::Oyster < Animal
+    def legs_in_oyster
+      LEGS
+    end
+  end
+
+  def test_who_wins_with_explicit_scoping_on_class_definition
+    assert_equal __, MyAnimals::Oyster.new.legs_in_oyster
+  end
+
+  # QUESTION: Now Which has precedence: The constant in the lexical
+  # scope, or the constant from the inheritance heirarachy?  Why is it
+  # different?
+end
diff --git a/src/about_constants.rb b/src/about_constants.rb
new file mode 100644 (file)
index 0000000..b0913a7
--- /dev/null
@@ -0,0 +1,87 @@
+require 'edgecase'
+
+C = "top level"
+
+class AboutConstants < EdgeCase::Koan
+
+  C = "nested"
+
+  def test_nested_constants_may_also_be_referenced_with_relative_paths
+    assert_equal __("nested"), C
+  end
+
+  def test_top_level_constants_are_referenced_by_double_colons
+    assert_equal __("top level"), ::C
+  end
+
+  def test_nested_constants_are_referenced_by_their_complete_path
+    assert_equal __("nested"), AboutConstants::C
+    assert_equal __("nested"), ::AboutConstants::C
+  end
+
+  # ------------------------------------------------------------------
+  
+  class Animal
+    LEGS = 4
+    def legs_in_animal
+      LEGS
+    end
+
+    class NestedAnimal
+      def legs_in_nested_animal
+        LEGS
+      end
+    end
+  end
+  
+  def test_nested_classes_inherit_constants_from_enclosing_classes
+    assert_equal __(4), Animal::NestedAnimal.new.legs_in_nested_animal    
+  end
+
+  # ------------------------------------------------------------------
+  
+  class Reptile < Animal
+    def legs_in_reptile
+      LEGS
+    end
+  end
+  
+  def test_subclasses_inherit_constants_from_parent_classes
+    assert_equal __(4), Reptile.new.legs_in_reptile
+  end
+  
+  # ------------------------------------------------------------------
+  
+  class MyAnimals
+    LEGS = 2
+
+    class Bird < Animal
+      def legs_in_bird
+        LEGS
+      end
+    end
+  end
+  
+  def test_who_wins_with_both_nested_and_inherited_constants
+    assert_equal __(2), MyAnimals::Bird.new.legs_in_bird
+  end
+  
+  # QUESTION: Which has precedence: The constant in the lexical scope,
+  # or the constant from the inheritance heirarachy?
+  
+  # ------------------------------------------------------------------
+  
+  class MyAnimals::Oyster < Animal
+    def legs_in_oyster
+      LEGS
+    end
+  end
+
+  def test_who_wins_with_explicit_scoping_on_class_definition
+    assert_equal __(4), MyAnimals::Oyster.new.legs_in_oyster
+  end
+
+  # QUESTION: Now Which has precedence: The constant in the lexical
+  # scope, or the constant from the inheritance heirarachy?  Why is it
+  # different?
+end
index a36f237..6a87bcb 100644 (file)
@@ -7,6 +7,7 @@ require 'about_array_assignment'
 require 'about_hashes'
 require 'about_strings'
 require 'about_methods'
+require 'about_constants'
 require 'about_control_statements'
 require 'about_true_and_false'
 require 'about_triangle_project'