मैं भूल गया है कि वहाँ रुबी में एक "वर्ग आवृत्ति चर" अवधारणा थी। किसी भी मामले में, ओपी की समस्या परेशान लग रही थी, और वास्तव में किसी भी उत्तर में संबोधित नहीं किया गया था, कोच के जवाब में संकेत के अलावा: यह दायरे की समस्या है। (संपादन पर जोड़ा गया: असल में, श्रीस का जवाब अंत में इस बिंदु को संबोधित करता है, लेकिन मुझे लगता है कि यह उत्तर समस्या को समझने के लिए उपयोगी हो सकता है।)
जहां कक्षा में यह कहा जाता है के आधार पर एक उदाहरण चर या एक वर्ग उदाहरण चर, करने के लिए करने के लिए या तो: रूबी वर्ग, एक चर नाम @
के साथ शुरू दो चर में से एक का उल्लेख कर सकते। यह एक काफी सूक्ष्म गोचा है।
एक उदाहरण बिंदु को स्पष्ट करेगा।
class T
@@class_variable = "BBQ"
@class_instance_variable_1 = "WTF"
@class_instance_variable_2 = "LOL"
def self.class_method
puts "@@class_variable == #{@@class_variable || 'nil'}"
puts "@class_instance_variable_1 == #{@class_instance_variable_1 || 'nil'}"
puts "@class_instance_variable_2 == #{@class_instance_variable_2 || 'nil'}"
puts "@instance_variable == #{@instance_variable || 'nil'}"
end
def initialize
@instance_variable = "omg"
# The following line does not assign a value to the class instance variable,
# but actually declares an instance variable withthe same name!
@class_instance_variable_1 = "wtf"
puts "@@class_variable == #{@@class_variable || 'nil'}"
# The following two lines do not refer to the class instance variables,
# but to the instance variables with the same names.
puts "@class_instance_variable_1 == #{@class_instance_variable_1 || 'nil'}"
puts "@class_instance_variable_2 == #{@class_instance_variable_2 || 'nil'}"
puts "@instance_variable == #{@instance_variable || 'nil'}"
end
def instance_method
puts "@@class_variable == #{@@class_variable || 'nil'}"
# The following two lines do not refer to the class instance variables,
# but to the instance variables with the same names.
puts "@class_instance_variable_1 == #{@class_instance_variable_1 || 'nil'}"
puts "@class_instance_variable_2 == #{@class_instance_variable_2 || 'nil'}"
puts "@instance_variable == #{@instance_variable || 'nil'}"
end
end
मैं मैं क्या सोचा था कि वे थे के अनुसार चर नाम है, कि हालांकि नहीं करने के लिए पता चला है हमेशा ऐसा होने के लिए: यह एक छोटा सा रूबी परीक्षण वर्ग (सभी कोड आईआरबी में परीक्षण किया गया) है
irb> T.class_method
@@class_variable == BBQ
@class_instance_variable_1 == WTF # the value of the class instance variable
@class_instance_variable_2 == LOL # the value of the class instance variable
@instance_variable == nil # does not exist in the class scope
=> nil
irb> t = T.new
@@class_variable == BBQ
@class_instance_variable_1 == wtf # the value of the instance variable
@class_instance_variable_2 == nil # the value of the instance variable
@instance_variable == omg
=> #<T:0x000000015059f0 @instance_variable="omg", @class_instance_variable_1="wtf">
irb> t.instance_method
@@class_variable == BBQ
@class_instance_variable_1 == wtf # the value of the instance variable
@class_instance_variable_2 == nil # the value of the instance variable
@instance_variable == omg
=> nil
irb> T.class_method
@@class_variable == BBQ
@class_instance_variable_1 == WTF # the value of the class instance variable
@class_instance_variable_2 == LOL # the value of the class instance variable
@instance_variable == nil # does not exist in the class scope
=> nil
@@class_variable
और @instance_variable
हमेशा व्यवहार करते हैं जैसा कि आप उम्मीद करेंगे: पूर्व वर्ग स्तर पर परिभाषित किया गया है, और चाहे किसी वर्ग विधि में या किसी उदाहरण विधि में संदर्भित किया गया हो, तो इसे शीर्ष पर असाइन किया गया मान रखता है। उत्तरार्द्ध केवल कक्षा T
के किसी ऑब्जेक्ट में एक मान प्राप्त करता है, इसलिए कक्षा विधि में, यह एक अज्ञात चर को संदर्भित करता है जिसका मान nil
है।
class_method
नाम की क्लास विधि @@class_variable
के मानों को आउटपुट करती है और दो @class_instance_variable
एस अपेक्षा की जाती है, जैसा कक्षा के शीर्ष पर शुरू किया गया है। हालांकि, उदाहरण के तरीकों में initialize
और instance_method
, एक ही नाम की अलग चर एक्सेस किया जाता है, वह है, उदाहरण चर, नहीं वर्ग उदाहरण चर।
आप देख सकते हैं कि initialize
विधि में काम, वर्ग उदाहरण चर @class_instance_variable_1
को प्रभावित नहीं किया क्योंकि class_method
के बाद कॉल अपने पुराने मूल्य, "WTF"
आउटपुट। इसके बजाय, विधि initialize
ने एक नया इंस्टेंस वैरिएबल घोषित किया, एक नामित (भ्रामक रूप से) @class_instance_variable_1
। इसे सौंपा गया मान, "wtf"
, विधियों initialize
और instance_method
द्वारा आउटपुट है।
चर @class_instance_variable_2
उदाहरण के कोड में वास्तविक समस्या में चर @hello
के बराबर है: यह घोषणा की और एक वर्ग उदाहरण चर के रूप में प्रारंभ है, लेकिन जब एक उदाहरण विधि है कि नाम के एक चर को संदर्भित करता है, यह वास्तव में देखता है एक उदाहरण एक ही नाम के साथ परिवर्तनीय - जिसे कभी घोषित नहीं किया गया था, इसलिए इसका मूल्य शून्य है।
आप कहते हैं, "वे पहली बार जीवन में वसंत करते हैं जब उन्हें सौंपा जाता है" फिर भी ओपी ने आपके उदाहरण के मुकाबले पहले (स्पष्ट) असाइनमेंट के साथ एक उदाहरण दिखाया, और अनुभव की गई समस्या यह है कि कहा गया चर वैसा ही जीवन में नहीं उभरा था। – kaleidic
@ kaleidic बिल्कुल। मैं इस उत्तर पर अपवॉट्स की संख्या पर थोड़ा परेशान हूं जो ओपी के प्रश्न को संबोधित नहीं करता है। वास्तव में, क्लास इंस्टेंस वैरिएबल '@ हैलो' * * उदाहरण कोड के पंक्ति 2 पर जीवन में वसंत करता है, लेकिन समस्या यह है कि वह चर नहीं है जो पंक्ति 4 को संदर्भित करता है। अधिक जानकारी के लिए नीचे मेरा जवाब देखें। –
क्षमा करें, आप वास्तव में अंत में प्रश्न का उत्तर देते हैं, जिसे मैंने पहले पढ़ने पर याद किया। –