CodeProject · Ruby

Safe guarding from NoMethodError exception in Ruby

Dear Reader,

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

So assume that if your writing your class and if some one 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 be 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 where in your dealing with a lot of classes and you may some times forget to fix every thing or some one 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, 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, 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 its some thing 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 πŸ™‚

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s