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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:22:26+00:00 2026-05-17T18:22:26+00:00

In Ruby, how can I programmatically determine which class/module defines a method being called?

  • 0

In Ruby, how can I programmatically determine which class/module defines a method being called?

Say in a given scope I invoke some_method(). In the same scope I’d like to invoke a function find_method(:some_method) that would return which Class, Singleton Class or Module defines some_method.

Here’s some code to illustrate what I mean:

class Foo
  include ...

  def bar
    ...
    some_method() # calls a method in scope, assume it exists

    find_method(:some_method) # Returns where the method is defined
                              # e.g. => SomeClassOrModule
    ...
  end
end

I’m guessing I have to use a complex mixture of reflective functions, starting with self and using self.ancestors to walk up the inheritance tree, using method_defined? to check if a method is defined on a Class or Module, and probably some other tricks to inspect scopes from the innermost to the outermost (since the code may run within, e.g., an instance_eval).

I just don’t know the correct order and all the subtleties of the Ruby metamodel to implement find_method such that it’s both exhaustive in its search and correct from a method dispatch resolution perspective.

thanks!

  • 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-17T18:22:27+00:00Added an answer on May 17, 2026 at 6:22 pm

    Found that this was surprisingly simple. Neat!

    Expanding on this answer, to show how to actually return the owner class:

    foo.method(:some_method).owner
    

    Wrap that in an instance method if you find it necessary, but that’s not really that bad, eh?

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

Sidebar

Related Questions

I know in Ruby can add and modify method of class dynamically in run
Ruby can add methods to the Number class and other core types to get
How can I do the Ruby method Flatten Ruby Method in C#. This method
How can I determine easily and programmatically whether a LocalJumpError arose from the caller's
Possible Duplicates: class << self idiom in Ruby Can someone please explain class <<
In ruby you can do this: class A def self.a 'A.a' end end puts
In Ruby you can reference variables inside strings and they are interpolated at runtime.
I need to generate a list of sequential numbers. I know Ruby you can
I'm trying to install some Ruby Gems so I can use Ruby to notify
I'd like to be able to write a ruby program that can restart without

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.