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

  • Home
  • SEARCH
  • 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 8969899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:39:31+00:00 2026-06-15T17:39:31+00:00

When I delegate methods on instances of class A to $delegation_target as below: $delegation_target

  • 0

When I delegate methods on instances of class A to $delegation_target as below:

$delegation_target = ""
class A
  def method_missing *args, ≺ $delegation_target.send(*args, &pr) end
  def respond_to_missing? *args; $delegation_target.respond_to?(*args) end
end

the arity of the methods on instances of A returns -1 irrespective of the arity of the methods on $delegation_target:

def $delegation_target.foo; end
A.new.method(:foo).arity # => -1

def $delegation_target.foo arg1; end
A.new.method(:foo).arity # => -1

def $delegation_target.foo arg1, arg2; end
A.new.method(:foo).arity # => -1

def $delegation_target.foo arg1, arg2, arg3; end
A.new.method(:foo).arity # => -1

Where does this -1 come from? And, is there a way to make it so that, for any possible method name m, A.new.method(m).arity returns the arity of $delegation_target.method(m) (if it is defined)?

  • 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-15T17:39:31+00:00Added an answer on June 15, 2026 at 5:39 pm

    Object#method handles respond_to_missing? & method_missing in a special way. Let’s dive into the Ruby C source and see what happens:

    Starting at Object#method, we call mnew, which creates a new Method object for the object it’s called on and the id passed. In the source for mnew we can easily see the special handling when the method is not defined, but respond_to_missing? returns true when given the id:

    if (UNDEFINED_METHOD_ENTRY_P(me)) {
        ID rmiss = rb_intern("respond_to_missing?");
        VALUE sym = ID2SYM(id);
    
        if (obj != Qundef && !rb_method_basic_definition_p(klass, rmiss)) {
            if (RTEST(rb_funcall(obj, rmiss, 2, sym, scope ? Qfalse : Qtrue))) {
                def = ALLOC(rb_method_definition_t);
                def->type = VM_METHOD_TYPE_MISSING;
                def->original_id = id;
                // ...
    

    The def->type = VM_METHOD_TYPE_MISSING; is important. Finding the definition for VM_METHOD_TYPE_MISSING, we see that it is a “wrapper for method_missing(id)“. So essentially the method that is returned is really just method_missing, with the first argument already specified as the id of the method you were originally trying to get.

    We can confirm our suspicions by verifying that the arity of method_missing is the same as what we’re getting:

    A.new.method(:method_missing).arity  #=> -1
    

    As an aside, an arity of -1 means that the method can take an unlimited number of arguments.

    As for whether you can have it return the “real” arity of the method being called… no, you can’t. For one, Ruby makes no assumptions about what happens in your method_missing, and doesn’t even know that it’s simply delegating to some other method.

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

Sidebar

Related Questions

Is it OK to put the CLLocationManager Delegate methods in a Singleton class which
I'm working with a static class that only delegate methods to an instance of
Given the UINavigationController delegate methods: -(void)navigationController:(UINavigationController*)navigationController (will/did)ShowViewController:(UIViewController*)viewController animated:(BOOL)animated How do you tell or compare
My UITextView delegate methods aren't firing. UILabel *fieldLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 290,
I'm trying to use the delegate methods of NSURLConnection. The following methods are currently
Is it possible to implement Delegate methods for FileSystemWatcher? I have a delegate method
Currently working with NSURLConnection. Found a great website showing important delegate methods coming with
When should anonymous methods be used when defining a delegate and when should formally
Is it possible to use the class methods of some NSObject I've made to
I have create a class MyConnection that extends NSURLConnection. I have implemented some delegate

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.