Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / Ruby

Safe Guarding from NoMethodError Exception in Ruby

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
1 Nov 2011LGPL32 min read 6.2K   1  
Safe guarding from NoMethodError exception in Ruby

We all know that Ruby is a dynamic language wherein calling a method on a type or perhaps object in Ruby is not known or figured out until run time.

So assume that if you are writing your class and if someone else is accessing your class and tries to call some method in your class, which may have been removed or modified or not at all exists and unfortunately the API document has not been assumed, then at run time, Ruby throws an exception called NoMethodError.

Although the above said problem/issue might sound not so important to you, but think in a large-scale enterprise application wherein you are dealing with a lot of classes and you may sometimes forget to fix everything or someone might have done the same. So rather than having exceptions at run time and making it a show stopper bug, you can write some safety net in your class code or in your project and making the whole app a bit more safer to work with.

Today, I learned this part of the topic and I hope you find it useful too as I did and I actually liked it too. So let me start by showing you a small code where there is no safety net implemented:

class MyClass
def SomeMethod

end

def SomeOtherMethod

end
end

MyClass.new.SomeOnesMethod

The above code will not display any problem when you build your project (I tried in netbeans, and no errors) but when you execute it, you get exceptions thrown by Ruby as “NoMethodError”.

To overcome this problem or in other words to implement a safety net, you need to override Kernel.method_missing(symbol, *args) method in your class, the below image shows the intellisense on the same:

Image 1

So upon implementing this method in the above code, I pretty much do not get any NoMethodError exceptions at run time, the below code shows the same:

class MyClass
def SomeMethod

end

def SomeOtherMethod

end

def method_missing(symbol, *args)
puts "Method name = #{symbol} is not present or missing with arguments #{args.inspect}"
end
end

MyClass.new.SomeRandomMethod(1,2,3)

Output: Method name= SomeOnesMethod is not present or missing with arguments [1, 2, 3]

So as you can see, the above code gives a normal output without any exception at all. So my application having this code is much safer now.

Allow me to explain the method_missing() method signature. The first argument symbol is a code block which gets the method name in it, if I can relate to C# .NET, then it's something like delegates. The second argument gets all the arguments passed to a method from caller side, actually the * prefixed to it denotes that it’s an array type which can accept 1 or more number of arguments. In the body of this method_missing() method, I am using Interpolation technique to print Symbol value and same with args array.

Happy coding! :)

P.S: Your valuable comments/votes are appreciated. :)

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Software Developer (Senior) Siemens
India India
A .net developer since 4+ years, wild, curious and adventurous nerd.

Loves Trekking/Hiking, animals and nature.

A FOSS/Linux maniac by default Wink | ;)

An MVP aspirant and loves blogging -> https://adventurouszen.wordpress.com/

Comments and Discussions

 
-- There are no messages in this forum --