Ruby Methods: how to return an usage string when insufficient arguments are given

Posted by Shyam on Stack Overflow See other posts from Stack Overflow or by Shyam
Published on 2010-04-14T14:50:41Z Indexed on 2010/04/14 15:33 UTC
Read the original article Hit count: 313

Hi,

After I have created a serious bunch of classes (with initialize methods), I am loading these into IRb to test each of them. I do so by creating simple instances and calling their methods to learn their behavior. However sometimes I don't remember exactly what order I was supposed to give the arguments when I call the .new method on the class. It requires me to look back at the code. However, I think it should be easy enough to return a usage message, instead of seeing:

ArgumentError: wrong number of arguments (0 for 9)

So I prefer to return a string with the human readable arguments, by example using "puts" or just a return of a string. Now I have seen the rescue keyword inside begin-end code, but I wonder how I could catch the ArgumentError when the initialize method is called.

Thank you for your answers, feedback and comments!

© Stack Overflow or respective owner

Related posts about ruby

Related posts about methods