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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:28:32+00:00 2026-05-18T10:28:32+00:00

I am reading this blog and came across following code RunAgain = Class.new(Exception) def

  • 0

I am reading this blog and came across following code

RunAgain = Class.new(Exception)
def fib(i, n = 1, result = 0)
  if i == -1
    result
  else
    raise RunAgain
  end
rescue RunAgain
  i, n, result = i - 1, n + result, n
  retry
end

It seems like for above code to work, once exception is raised then ruby must be clearing the whole stack trace and replacing that with the stack of Exception.

Is my understanding right?

  • 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-18T10:28:33+00:00Added an answer on May 18, 2026 at 10:28 am

    The way this code works actually has nothing to do with the stacktrace. There are two entries on the stack the entire time, fib and the caller of fib. The raise of the exception is sort of a red herring to your question–it doesn’t serve any useful purpose in this example other than as a demonstration of the behavior of retry.

    Ruby’s retry is similar to other control keywords like next and break, and redo. The redo keyword means to retry the current loop or block from the top. The retry keyword works inside a rescue to retry the current block that threw the exception.

    So what happens here is some initial values are set for i, n, and result, a base case is checked (i == -1), and if not satisfied, we update the values and retry from the top. Notice that since these values are method parameters and not local variables, they are not reinitialized.

    Careful, since Fibonacci is a very common example (and a very poor one) of recursion, not to mistake this for a recursive algorithm. The RunAgain raise and rescue functions like a loop, without re-calling the function or modifying the callstack.

    Your example code is equivalent to

    def fib(i)
      n, result = 1, 0
      (i+1).times { n, result = n + result, n }
      result
    end
    

    Note in both cases, i is just a counter and nothing more. We run the code i+1 times. Note also the typical need for a temporary variable to swap values is replaced by ruby’s multiple assignment construct.

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

Sidebar

Related Questions

I was reading this blog post: http://www.shakie.co.uk/ramblings/feature-driven-development-riding-the-waves-of-change/ and came across the part about TestDox:
Reading this blog post about HttpOnly cookies made me start thinking, is it possible
Reading this question I found this as (note the quotation marks) code to solve
Just finished reading this blog post: http://www.skorks.com/2010/03/an-interview-question-that-prints-out-its-own-source-code-in-ruby/ In it, the author argues the case
This question came to my mind while I am reading the post Why doesn't
I was reading about data driven testing using mbunit from this article. http://blog.benhall.me.uk/2007/04/mbunit-datafixture-data-driven-unit.html I
Reading this post has left me wondering; are nightly builds ever better for a
After reading this question , I was reminded of when I was taught Java
After reading this description of late static binding (LSB) I see pretty clearly what
After reading this answer: best way to pick a random subset from a collection?

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.