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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T11:17:41+00:00 2026-05-28T11:17:41+00:00

I am wondering: is there a way to call a method from within itself

  • 0

I am wondering: is there a way to call a method from within itself in Ruby without using its name?

If the method was created by some metaprogramming techniques, then calling it by its name may be hard to read. Even for a usually defined method, if you are not sure about a good name for it, or if its name is long, calling it from within itself by some keyword (analogous to super) may be convenient.

  • 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-28T11:17:41+00:00Added an answer on May 28, 2026 at 11:17 am

    You can use Kernel#__method__ that returns the name of the current method as a Symbol. Unlike super it’s not a keyword but a regular method so you have to pass it to send method along with required arguments in order to call the method.

    Here is what __method__ returns:

    obj = Object.new
    
    def obj.foo
      p __method__
    end
    
    obj.foo
    # => :foo
    

    And here is an example of class method that dynamically defines factorial methods:

    class Foo
      def self.define_fact(method_name)
        define_method(method_name) do |n|
          n > 0 ? n * send(__method__, n - 1) : 1
        end
      end
    end
    
    f = Foo.new
    # puts f.fact(5) 
    # => undefined method `fact' for #<Foo:0x8ede45c> (NoMethodError)
    Foo.define_fact :fact
    puts f.fact(5)
    # => 120 
    

    Without __method__ I can’t think of any solution that wouldn’t involve some kind of eval that is better to avoid if possible.

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

Sidebar

Related Questions

I am wondering if there is some way to call C++ code from Common
I was wondering if there was a way to call existing C code from
Im just wondering whether there is a way to call a method where i
how to call an AppDelegate method from RootViewController without passing a delegate? Just wondering
I was wondering if there is way to open another page using a Modal
Wondering if there is any way to get the lambda expressions that result from
I was wondering if there is any possible way to call functions in a
Is there a way to call method inside a running .NET assembly. The problem
I was wondering if there was a way to stop an iPad popover from
I am wondering if there is some way to look at the parameter list

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.