Ruby: NoMethodError, but why?

I worked on a simple Pi generator while learning Ruby, but I continued to get NoMethodError on RubyMine 6.3.3, so I decided to create a new project and a new class as simple as possible, and I STILL get NoMethodError. Any reason?

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

And I get the error:

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

You have defined an instance method and are trying to name it as a class method. Thus, you need to make the hello method a class method, not an instance method of the Methods class.

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

Or, if you want to define it as an instance method, call it as shown below:

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

You are trying to call an instance method as a class method.

Here is some code that illustrates the difference between the two in ruby:

 class Person # This is a class method - note it 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 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 

Now try calling the methods ...

 # 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
source

You created an instance method, but you call a class method. To call hello("Annie") , you need to instantiate the methods. For instance:

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

This will lead to the conclusion Hello from Annie

+1
source

By defining a method with def method_name args , you define an instance method that will be included in every object of this class, but not in the class itself.

On the other hand, with def self.method_name args you get a class method that will be directly in the class, without having to run an object from it.

So, if you have this:

 Class Test def self.bar end def foo end end 

You can execute the instance method as follows:

 a = Test.new a.foo 

As for the class, you need:

 Test.foo 
0
source

Source: https://habr.com/ru/post/1204639/


All Articles