2014-10-14 7 views
5

मैं रूबी सीखते समय एक साधारण पीआई जेनरेटर पर काम कर रहा था, लेकिन मुझे रूबीमाइन 6.3.3 पर नोमूथररर मिल रहा था, इसलिए मैंने एक नई परियोजना और नई कक्षा को यथासंभव सरल बनाने का फैसला किया, और मैं STILL NoMethodError प्राप्त करें। कोई कारण?रूबी: NoMethodError, लेकिन क्यों?

class Methods 
    def hello (player) 
    print "Hello, " << player 
    end 
    hello ("Annie") 
end 

और त्रुटि मैं मिलता है:

C:/Users/Annie the Eagle/Documents/Coding/Ruby/Learning Environment/methods.rb:5:in `<class:Methods>': undefined method `hello' for Methods:Class (NoMethodError) 
+0

आप वर्ग परिभाषा के अंदर विधि कॉल कर रहे हैं होगा। मैं इसके इरादे से अनिश्चित हूँ। – Tensibai

+0

मुझे ओओ प्रोग्रामिंग में आपकी पृष्ठभूमि/अनुभव के स्तर की जानकारी नहीं है, इसलिए बस जांचें: क्या आप कक्षाओं और उदाहरणों (और कक्षा विधियों और उदाहरण विधियों) के बीच अंतर को समझते हैं? जवाब आपको लगता है कि ... –

उत्तर

7

आप एक उदाहरण विधि को परिभाषित किया है और एक वर्ग के तरीके के रूप में कॉल करने का प्रयास कर रहे हैं। इस प्रकार आपको hello विधि वर्ग विधि बनाने की आवश्यकता है, कक्षा Methods की एक उदाहरण विधि नहीं है।

class Methods 
    def self.hello(player) 
    print "Hello, " << player 
    end 
    hello("Annie") 
end 

या, यदि आप उदाहरण विधि के रूप में यह निर्धारित करना चाहते हैं तो यह नीचे के रूप में कहते हैं:

class Methods 
    def hello(player) 
    print "Hello, " << player 
    end 
end 
Methods.new.hello("Annie") 
+0

ओह वाह ... तो self.hello "स्थिर (वापसी प्रकार) हैलो" के बराबर सी # के समान होगा? "डेफ हैलो" का उपयोग करने का अधिकार कब होगा –

+0

आपको विधि नाम और ब्रांडेसिस के बीच की जगह को हटा देना चाहिए। – Stefan

+0

@Stefan आपको बहुत बहुत धन्यवाद! –

0

के साथ एक विधि को परिभाषित करके डीईएफ़ आर्ग पद्वति आप एक उदाहरण विधि को परिभाषित कर रहे हैं कि उस वर्ग के हर वस्तु में शामिल किया जाएगा, लेकिन कक्षा में ही नहीं।

दूसरी ओर, def self.method_name args आपको एक क्लास विधि मिलेगी जो सीधे उस वर्ग में होगी, बिना किसी ऑब्जेक्ट को इंस्टाल करने की आवश्यकता के।

तो अगर आप इस राशि:

Class Test 
    def self.bar 
    end 

    def foo 
    end 

end 

आप उदाहरण विधि इस तरह से अमल कर सकते हैं:

a = Test.new 
a.foo 

और वर्ग के लिए के रूप में एक होना चाहिए:

Test.foo 
1

आप एक क्लास विधि के रूप में एक उदाहरण विधि को कॉल करने की कोशिश कर रहा हूं।

class Person 

    # This is a class method - note it's prefixed by self 
    # (which in this context refers to the Person class) 
    def self.species 
    puts 'Human' 
    # Note: species is OK as a class method because it's the same 
    # for all instances of the person class - ie, 'Bob', 'Mary', 
    # 'Peggy-Sue', and whoever else, are ALL Human. 
    end 

    # The methods below aren't prefixed with self., and are 
    # therefore instance methods 

    # This is the construct, called automatically when 
    # a new object is created 
    def initialize(name) 
    # @name is an instance variable 
    @name = name 
    end 

    def say_hello 
    puts "Hello from #{@name}" 
    end 

end 

और अब इसे आज़माने, विधियों बुला ...

# Call a class method... 
# We're not referring to any one 'instance' of Person, 
Person.species #=> 'Human' 

# Create an instance 
bob = Person.new('Bob') 

# Call a method on the 'Bob' instance 
bob.say_hello #=> 'Hello from Bob' 

# Call a method on the Person class, going through the bob instance 
bob.class.species #=> 'Human' 

# Try to call the class method directly on the instance 
bob.species #=> NoMethodError 

# Try to call the instance method on the class 
# (this is the error you are getting) 
Person.say_hello #=> NoMethodError 
1

आप एक उदाहरण विधि बना लिया है:

यहाँ कुछ कोड है कि रूबी में दोनों के बीच अंतर को दिखाता है , लेकिन आप एक क्लास विधि कॉल कर रहे हैं। hello("Annie") पर कॉल करने के लिए, आपको विधियों का एक उदाहरण बनाना होगा। उदाहरण के लिए:

class Methods 
    def self.hello(player) 
     print "Hello, " << player 
    end 
end 

my_method = Methods.new 
my_method.hello("Annie) 

यह आउटपुट Hello from Annie

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