2011-03-28 21 views
19

पर स्पष्ट स्कॉइंग मैं इस मूल post से कुछ चीजों को स्पष्ट करना चाहता था। जवाब सुझाव दिया है कि रूबी इस क्रम में निरंतर परिभाषा की खोज करता है:रूबी कोअन्स: कक्षा परिभाषा भाग 2

  1. संलग्न गुंजाइश
  2. किसी भी बाहरी स्कोप (शीर्ष स्तर तक पहुँच जाता है दोहराने)
  3. शामिल मॉड्यूल
  4. सुपर क्लास (ते)
  5. वस्तु
  6. कर्नेल

तो स्पष्ट करने के लिए, पर 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 
+0

किसी ने पहले इस koan के बारे में पूछा है:

FYI करें, मैं इस जानकारी के लिए निम्न URL से (प्रश्न में संदर्भित आप लिंक करने के लिए) मिला/प्रश्न/4627735/रूबी-स्पष्ट-स्कॉइंग-ऑन-ए-क्लास-परिभाषा –

+0

@Andrew - मैंने पोस्ट में निर्दिष्ट किया है। मैं सिर्फ इस विषय पर और चर्चा करने की कोशिश कर रहा हूं क्योंकि ऐसे कुछ भाग हैं जिन्हें मैं समझ नहीं पाया। क्या मुझे वहां टिप्पणी करनी चाहिए? –

+0

क्षमा करें, मैंने यह ध्यान नहीं दिया। –

उत्तर

31

मैं केवल उसी ही प्रश्न से उसी प्रश्न पर विचार कर रहा था। मैं स्कोपिंग में कोई विशेषज्ञ नहीं हूं, लेकिन निम्नलिखित सरल स्पष्टीकरण ने मुझे बहुत समझदारी दी है, और शायद यह आपकी भी मदद करेगा।

जब आप MyAnimals::Oyster परिभाषित आप, वैश्विक क्षेत्र में अभी भी इतने गहरे लाल रंग का MyAnimals में 2 करने के लिए सेट है क्योंकि आप वास्तव में कभी MyAnimals के दायरे में हैं LEGS मूल्य का ज्ञान नहीं (एक छोटे से counterintuitive) है।

class MyAnimals 
    class Oyster < Animal 
    def legs_in_oyster 
     LEGS # => 2 
    end 
    end 
end 

अंतर यह है कि उपरोक्त कोड में, बार जब आप Oyster को परिभाषित करके, आप MyAnimals के दायरे में गिरा दिया है:

हालांकि, चीजों को अलग करता है, तो आप Oyster इस तरह से परिभाषित करने के लिए थे होगा , इसलिए रूबी जानता है कि LEGSMyAnimals::LEGS (2) और Animal::LEGS (4) को संदर्भित करता है। http://stackoverflow.com:

+0

धन्यवाद @bowsersenior! मैंने उस लिंक को देखा लेकिन काफी दूर नहीं पढ़ा। उपयोगकर्ता त्रुटि –

+0

मुझे लगता है कि यह 'Oyster

+0

होना चाहिए @AndreyBotalov! इसे सही करने के लिए कोड अपडेट किया गया। – bowsersenior

संबंधित मुद्दे