I wanted to clarify some things off of this original post. The answer suggested that Ruby searches for the constant definition in this order:
- The enclosing scope
- Any outer scopes (repeat until top level is reached)
- Included modules
- Superclass(es)
- Object
- Kernel
So to clarify, at which step (1-6) is the value for the constant LEGS
found for legs_in_oyster
? Is it from the Superclass Animal
? Is the scope of class MyAnimals
ignored because it is not considered an enclosing scope? Is this due to the explicit MyAnimals::Oyster
class definition?
Thanks! Just trying to understand. Here is the 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 < 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 than the previous answer?
end