Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 737817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:45:55+00:00 2026-05-14T07:45:55+00:00

After I have created a serious bunch of classes (with initialize methods), I am

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T07:45:55+00:00Added an answer on May 14, 2026 at 7:45 am

    It is possible to hook into object creation by overriding the Class#new method e.g.

    class Class
      # alias the original 'new' method before overriding it
      alias_method :old_new, :new
      def new(*args)
        return old_new(*args)
        rescue ArgumentError => ae          
          if respond_to?(:usage)
            raise ArgumentError.new(usage)
          else 
            raise ae
          end
      end
    end
    

    This overriden method calls the normal new method but catches ArgumentError and if the class of the object being created provides a usage method then it will raise an ArgumentError with the usage message otherwise it will reraise the original ArgumentError.

    Here is an example of it in action. Define a Person class:

    class Person
      def initialize(name, age)
      end
    
      def self.usage
        "Person.new should be called with 2 arguments: name and age"
      end
    end
    

    and then try and instantiate it without the required arguments:

    irb(main):019:0> p = Person.new
    ArgumentError: Person.new should be called with 2 arguments: name and age
        from (irb):8:in `new'
        from (irb):22
    

    Note: this isn’t perfect. The main problem being that it is possible that the ArgumentError we catch has been caused by something other than an incorrect number of arguments being passed to initialize which would lead to a misleading message. However it should do what you want in most cases.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

should i after i have created a svn repo, import one project folder into
I'm a bit unfamiliar with updating SQLite Databases after I have created them. Is
i have created one login and register view in that view after login user
I have created a parent div and inserted div's after the parent div using
I have created SQL Server 2008 Database project using visual studio 2010. after build
I have created an html5 application in which I open an pdf, after opening
I have created a cube that worked fine when deploying. After a added another
I have created a application in c# , it reads excel file and after
I have created firstly ASP.NET MVC 2 . and write more functionality. After I
we have created a VSTO Excel workbook with VS2010, .NET 4.0. After we publish

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.