Click here to Skip to main content
13,046,281 members (114,143 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


1 bookmarked
Posted 1 Nov 2011

Safe Guarding from NoMethodError Exception in Ruby

, 1 Nov 2011
Rate this:
Please Sign up or sign in to vote.
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


def SomeOtherMethod


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:

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


def SomeOtherMethod


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

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. :)


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


About the Author

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

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170713.1 | Last Updated 1 Nov 2011
Article Copyright 2011 by zenwalker1985
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid