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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:24:31+00:00 2026-05-31T12:24:31+00:00

Often, I have a recursive routine within a method that is only called by

  • 0

Often, I have a recursive routine within a method that is only called by that method or within the recursive routine itself:

def foo
  ...
  bar
  ...
end

def bar
  ...
  bar
  ...
end

But since bar is not used anywhere else, I do not want to define it as a method but somehow put it inside the method that calls it like this:

def foo
  ...
  bar {# some way to mark the recursive routine
    ...
    bar # some way to call the recursive routine
    ...
  }
  ...
end

Is this possible?

  • 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-31T12:24:32+00:00Added an answer on May 31, 2026 at 12:24 pm

    Easy with a lambda/proc:

    def foo(n)
      fact = lambda do |i|
        i.zero? ? 1 : i * fact.call(i-1)
      end
      fact.call(n)
    end
    
    foo(4) # => 24
    

    You can also use a protected or private method.

    If performance is a real concern, creating an object (the lambda) each time will be slower, and calling a lambda is also slower, closure and all. My fruity gem gives me a 3.3x slowdown on this trivial example; the penalty should be much less for cases that actually do something more involved. Just be sure that performance really is an issue; you know what they say about premature optimization…

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

Sidebar

Related Questions

I often have to deal with XML documents that contain namespaced elements, but doesn't
I often have data in Excel or text that I need to get into
I often have a subroutine in Perl that fills an array with some information.
I have found that I often have to implement some sort of a scheduler
I have an MVC view with a partial view recursive call that displays hierarchical
I have noticed that with Delphi 2009, I often get strange errors when compiling,
When creating system services which must have a high reliability, I often end up
I often have to write queries with a fairly complex, constructed column that I
I often have to work with fragile legacy websites that break in unexpected ways
I have been messing around with recursion today. Often a programming technique that is

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.