title: Ruby Koans: explicit scoping on a class definition part 2
body: <p>I wanted to clarify some things off of this original <a href="https://stackoverflow.com/questions/4627735/ruby-explicit-scoping-on-a-class-definition/4629832#4629832">post</a>. The answer suggested that Ruby searches for the constant definition in this order:</p>
  
  <ol>
  <li>The enclosing scope</li>
  <li>Any outer scopes (repeat until top level is reached)</li>
  <li>Included modules</li>
  <li>Superclass(es)</li>
  <li>Object</li>
  <li>Kernel</li>
  </ol>
  
  <p>So to clarify, at which step (1-6) is the value for the constant <code>LEGS</code> found for <code>legs_in_oyster</code>? Is it from the Superclass <code>Animal</code>? Is the scope of class <code>MyAnimals</code> ignored because it is not considered an enclosing scope? Is this due to the explicit <code>MyAnimals::Oyster</code> class definition? </p>
  
  <p>Thanks! Just trying to understand. Here is the code:</p>
  
  <pre><code> 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 MyAnimals
     LEGS = 2
  
     class Bird &lt; 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 &lt; 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 than the previous answer?
   end
  </code></pre>