title: Ruby scopes, constants precedence: lexical scope or inheritance tree
body: <p>I'll show your the code snippet from <a href="http://rubykoans.com/" rel="noreferrer" title="rubykoans">rubykoans</a> tutorial. Consider the next code:</p>
  
  <pre><code>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 hierarchy?
  # ------------------------------------------------------------------
  
  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 hierarchy?  Why is it
  # **different than the previous answer**?
  </code></pre>
  
  <p>Actually the question is in comments (I highlighted it with asteriks (though it intended to be in bold)). Can anybody explain me please? Thanks in advance!</p>