rubykoans 튜토리얼에서 코드 스 니펫을 보여 드리겠습니다. 다음 코드를 살펴 보자 : (가) 굵게 의도하지만 (나는 asteriks으로 강조) Ruby 스코프, 상수 우선 순위 : 어휘 범위 또는 상속 트리
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 hierarchy?
# ------------------------------------------------------------------
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 hierarchy? Why is it
# **different than the previous answer**?
는 사실 문제는 의견이다. 누가 제발 설명해 주실 래요? 미리 감사드립니다!