The NoMethodError
is the most common error encountered in Ruby. As the name suggests, a NoMethodError
occurs when the object on which we are trying to call a method or an attribute is not defined.
For example when you call a method on an object that is nil
or that is not defined:
message = "hello world"
message.dummyMethod
Output:
undefined method `dummyMethod' for "hello world":String (NoMethodError)
Let’s take a look at some more examples and how to define a method_missing()
method to gracefully handle such errors.
Example 1: NoMethodError in Ruby
In the code example that follows, we've built a calculator that can operate on two numbers. In order to do these activities, we created the class' objects and called the various methods.
class User:
def plus(a,b)
puts "The sum is : #{a} + #{b} = #{a+b}"
end
def mul(a,b)
puts "The product is : #{a} * #{b} = #{a*b}"
end
end
user = User.new()
user.plus(3,4)
user.mul(5,6)
user.divide(4,5)
Output: Example 1
When the above code is executed we get the following output:
The sum is : 3 + 4 = 7
The product is : 5 * 6 = 30
undefined method `divide' for #<User:0x00562057fe52e0> (NoMethodError)
You can see that for the first two methods our code gave the correct answer but for the divide()
method we got NoMethodError
. This indicates that the method which we are calling using the object user
isn’t defined.
Use method_missing() to Handle NoMethodError in Ruby
When dealing with small source code like the above, it’s easy to just check the methods and trace the error, but when dealing with a huge code base, we need an efficient way to deal with this. That’s where method_missing()
comes into the picture.
We can define a method_missing()
method in our class. This method works like a delegation circumstance, so whenever in the code a method is called which otherwise throws a NoMethodError
, themethod_missing()
is invoked instead. The method_missing()
acts as a catch-all statement and sends the "missing" method calls to other objects.
Since it's conceivable that, in the method_missing()
, the object we're calling is a member of another class, in real-world projects we typically utilize the objects of other classes to search for a method in other classes. However, the choice of what is contained in the method_missing()
is left to the developers.
Example 2: Handling NoMethodError in Ruby
Let’s try to add this method to our existing code from example 1:
class User:
def plus(a,b)
puts "The sum is : #{a} + #{b} = #{a+b}."
end
def mul(a,b)
puts "The product is : #{a} * #{b} = #{a*b}."
end
def method_missing(method, *args)
puts "Sorry, I don't know any #{method} method."
end
end
user = User.new()
user.plus(3,4)
user.mul(5,6)
user.divide(4,5)
Output: Example Two
The sum is : 3 + 4 = 7.
The product is : 5 * 6 = 30.
Sorry, I don't know any divide method.
Track, Analyze and Manage Errors With Rollbar
Managing errors and exceptions in your code is challenging. It can make deploying production code an unnerving experience. Being able to track, analyze, and manage errors in real-time can help you proceed with more confidence. Rollbar automates error monitoring and triaging, making fixing Ruby errors easier than ever. Try it today!