I wanted to clarify some things from this origin. The answer is suggested that Ruby for constant definition in this order:
- attached scope
- any external scope (Repeat until it reached the top level )
- module
- object
- kernel
To clarify, at what phase (1-6) is found continuous
LEGS
legs_in_oyster ? Is this superclass
animal ? Is the class
MyAnimals been ignored because it is not considered an attached scope? Is this clear because of the definition of
MyAnimals :: Oyster class?
Thank you! Just trying to understand here is the code:
class animal lags = 4 def pi_in_anel legs and class nestedAnimal def pI_Insted_Animal Legs and end FI test_nested_classes_inherit_constants_from_enclosing_classes assert_equal 4, animal :: nested enmil.new Legs_in_nested_animal and # ----------------------------------------------- ------ ------------- Class MyAnimals Legs = 2 square birds & lt; Animal def 2 assert_equal foot end end end def test_who_wins_with_both_nested_and_inherited_constants legs_in_bird, MyAnimals :: Bird.new.legs_in_bird end # Question: Which precedence: the literal scope, consistent with persistent or legacy heirarachy in #? # ------------------------------------------------- ----------------- Class MyAnimals :: Oyster & lt; Animals def foot end end legs_in_oyster def test_who_wins_with_explicit_scoping_on_class_definition assert_equal 4, MyAnimals :: Oyster.new.legs_in_oyster end # Question: Which is now the precedence: continuous in literal # realm, or continuous from heritage heirarachy? Why is it different # compared to the previous answer? Finally
I was very expensive very question of a cone. I'm an expert in scoping, but the following simple explanation has made a lot of sense for me, and maybe it will help you along side. The
When you define
MyAnimals :: oyster you have no knowledge of
legs still so deep red color, global field value 2 Is set to
MyAnimals because you are actually within the scope of
MyAnimals (slight oddity).
However, if you want to define
Oyster , then things are different:
class MyAnimals class Oyster & lt; Animal def pie_in_yoster leg # = & gt; 2 and and and
The difference is that in the code above when you define
Oyster are, as long as you
MyAnimals , Then it is dark red that knows
feet
MyAnimals :: foot (2) and not
animal :: foot ( 4). / Li>
Comments
Post a Comment