पर स्पष्ट स्कॉइंग मैं इस मूल post से कुछ चीजों को स्पष्ट करना चाहता था। जवाब सुझाव दिया है कि रूबी इस क्रम में निरंतर परिभाषा की खोज करता है:रूबी कोअन्स: कक्षा परिभाषा भाग 2
- संलग्न गुंजाइश
- किसी भी बाहरी स्कोप (शीर्ष स्तर तक पहुँच जाता है दोहराने)
- शामिल मॉड्यूल
- सुपर क्लास (ते)
- वस्तु
- कर्नेल
तो स्पष्ट करने के लिए, पर legs_in_oyster
के लिए स्थिर LEGS
के लिए कौन सा चरण (1-6) मान है? क्या यह सुपरक्लास 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
किसी ने पहले इस koan के बारे में पूछा है:
FYI करें, मैं इस जानकारी के लिए निम्न URL से (प्रश्न में संदर्भित आप लिंक करने के लिए) मिला/प्रश्न/4627735/रूबी-स्पष्ट-स्कॉइंग-ऑन-ए-क्लास-परिभाषा –
@Andrew - मैंने पोस्ट में निर्दिष्ट किया है। मैं सिर्फ इस विषय पर और चर्चा करने की कोशिश कर रहा हूं क्योंकि ऐसे कुछ भाग हैं जिन्हें मैं समझ नहीं पाया। क्या मुझे वहां टिप्पणी करनी चाहिए? –
क्षमा करें, मैंने यह ध्यान नहीं दिया। –