이 원본의 내용을 명확히하고 싶습니다. post. 대답은 루비가이 순서에 상수 정의를 검색 제안 :Ruby Koans : 클래스 정의 부분의 명시적인 범위 지정 2
- 커널
그래서 명확하게
LEGS
에 대한 값입니까? 수퍼 클래스 Animal
에서 왔습니까? 둘러싸는 범위로 간주되지 않으므로 클래스 MyAnimals
의 범위가 무시됩니까? 이것은 명시적인 MyAnimals::Oyster
클래스 정의 때문입니까? 감사합니다. 이해하려고 애를 써. 코드는 다음과 같습니다.
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
:
참고로, 나는 (당신이 링크 된 질문에서 참조로)는 다음 URL에서이 통찰력을 가지고/questions/4627735/ruby-explicit-scope-on-a-class 정의 –
@Andrew - 게시물에 명시했습니다. 나는 이해하지 못했던 부분이 있기 때문에 주제에 대한 더 많은 토론을하려고 노력하고 있습니다. 나는 단지 거기에 코멘트해야합니까? –
미안하지만, 나는 그것을 알아 차리지 못했다. –