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 7974803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:28:58+00:00 2026-06-04T08:28:58+00:00

I want to do something but I’m not sure if it is possible. I

  • 0

I want to do something but I’m not sure if it is possible. I want to use “generic methods” or “default methods” in case when some method is called but is not defined. This is a simple example so you can understand my point:

This is the class:

class XYZ

    def a
        #...
    end

    def b
        #...
    end
end

The instance of the class XYZ:

n = XYZ.new
n.a
n.b
n.c

As you can see, I’m calling the method “c” which is not defined and it will throw an error. Can I do something in the class XYZ so when someone calls a method not defined get the name of the method and do something, in base of the name of the method? And, is this possible in another languages (not making a compiler)? If this is possible, how is it called (theory speaking)?

  • 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-06-04T08:29:00+00:00Added an answer on June 4, 2026 at 8:29 am

    Use method_missing:

    class XYZ
      def a; end
      def b; end
    
      def method_missing(name, *args)
        "Called #{name} with args: #{args}"
      end
    end
    
    XYZ.new.c #=> "Called c"
    

    You should also define respond_to_missing? to get respond_to? to work nicer in 1.9.2+. You should read more about respond_to?/respond_to_missing? when using method_missing.

    This, by the way, would be considered to be metaprogramming. This isn’t typically possible in compiled languages because of the way they call functions.

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

Sidebar

Related Questions

I do not know whether it is possible but i want something like <f:view>
I want to do something but not sure how to describe it. I have
want to ask user to input something but not want to wait forever. There
I want something similar with php/jsp in java, but not on the server side,
which STL Algorithm should i use if I want to find something but Don't
i just want to use an entity modify it to show something,but don't want
I want to do something but cant figure out how to do this (i
I want something like the following but would like it to be reusable for
I want something like a canvas, but where i'd be able to manipulate pixels
Someone posted something similar but it didn't really solve the problem. I want to

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.