Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby: NoMethodError, but why?

I was working on a simple Pi Generator while learning Ruby, but I kept getting NoMethodError on RubyMine 6.3.3, so I decided to make a new project and new class with as simple as possible, and I STILL get NoMethodError. Any reason?

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

And the error I get is:

C:/Users/Annie the Eagle/Documents/Coding/Ruby/Learning Environment/methods.rb:5:in `<class:Methods>': undefined method `hello' for Methods:Class (NoMethodError)
like image 258
Annie the Eagle Avatar asked Oct 14 '14 10:10

Annie the Eagle


People also ask

What is NoMethodError in Ruby?

Raised when a method is called on a receiver which doesn't have it defined and also fails to respond with method_missing .

How do you make a method private in Ruby?

The keyword private tells Ruby that all methods defined from now on, are supposed to be private. They can be called from within the object (from other methods that the class defines), but not from outside.

What is undefined in Ruby?

The undefined method is also called the NoMethodError exception, and it's the most common error within projects, according to The 2022 Airbrake Error Data Report. It occurs when a receiver (an object) receives a method that does not exist.

How do you define a class method in Ruby?

Class Methods are the methods that are defined inside the class, public class methods can be accessed with the help of objects. The method is marked as private by default, when a method is defined outside of the class definition. By default, methods are marked as public which is defined in the class definition.


2 Answers

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

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

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

class Methods
  def hello(player)
    print "Hello, " << player
  end
end
Methods.new.hello("Annie")
like image 198
Arup Rakshit Avatar answered Oct 11 '22 17:10

Arup Rakshit


You're trying to call an instance method as a class method.

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

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

And now try it out, 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
like image 22
joshua.paling Avatar answered Oct 11 '22 15:10

joshua.paling