Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Safe guarding from NoMethodError exception in Ruby

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

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

License

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

Share

About the Author

zenwalker1985
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/
Follow on   Twitter

Comments and Discussions

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