Skip to content Skip to sidebar Skip to footer

Print Something Then Go Back to Function Then Come Back Again Ruby

academy

Ruby's redo, retry and next keywords

We've talked about the retry keyword while discussing retrying after exceptions. Its little-known counterpart redo works similarly, but re-runs loop iterations instead of whole blocks.

The redo keyword

As we learned in the previous Academy article, retry lets you retry a piece of code in a block:

                                                            begin                                                                                                          puts                                                                  "Iteration"                                                                                                          raise                                                              rescue                                                                                                          retry                                                              end                                                      

This example prints the word "Iteration" to the console before raising an exception. The rescue blocks executes, which calls retry and starts the block again from the beginning. This results in our program endlessly printing Iteration. The redo keyword lets you achieve a similar effect when using loops. This is useful in situations where you need to retry while iterating for example.

                                                            10                      .times                                            do                                              |                      i                      |                                                                                                          puts                                                                  "Iteration                                            #{                      i                      }                      "                                                                                                          redo                                                                  if                                              i                                            >                                                                  2                                                              end                                                      

This will print:

                $ ruby redo.rb Iteration 0 Iteration 1 Iteration 2 Iteration 3 Iteration 3 Iteration 3 ...                              

Notice how the iteration count stays the same? It will step back execution to the start of the loop. This variant of the code using retry will print the exact same output:

                                                            10                      .times                                            do                                              |                      i                      |                                                                                                          begin                                                                                                          puts                                                                  "Iteration                                            #{                      i                      }                      "                                                                                                          raise                                                                  if                                              i                                            >                                                                  2                                                                                                          rescue                                                                                                          retry                                                                                                          end                                                              end                                                      

You can use redo to implement retrying in a loop. In the next example we have a queue of jobs. They either return :success or :failure. We keep re-running the same iteration of the loop until the job succeeds.

                                                            [job_1, job_2, job_3, job_4].each                                            do                                              |                      job                      |                                                                                                          redo                                                                  unless                                              job.call                                            ==                                                                  :                      success                                                              end                                                      

The next keyword

If you want to move to the next iteration of the loop, as opposed to moving back to the start of the current one, you can use next.

                                                            10                      .times                                            do                                              |                      i                      |                                                                                                          puts                                                                  "Iteration                                            #{                      i                      }                      "                                                                                                          next                                                                  if                                              i                                            >                                                                  2                                                                                                          puts                                                                  "Iteration done"                                                              end                                                      

This will print:

                $ ruby next.rb Iteration 0 Iteration done Iteration 1 Iteration done Iteration 2 Iteration done Iteration 3 Iteration 4 ...                              

See how the iteration counter keeps incrementing? In most cases using next is what you want. Look at redo if you need a loop that runs an exact number of times or needs error handling when iterating over an array.

We hope you learned something new about redoing iterations in loops and would love to know what you thought of this article (or any of the other ones in the AppSignal Academy series). Please don't hesitate to let us know what you think, or if you have any Ruby subjects you'd like to learn more about.

Write for our blog

Would you like to contribute to the AppSignal blog? We're looking for skilled mid/senior-level Ruby, Elixir, and Node.js writers.

Find out more and apply

Share this article

Thijs Cadier

Thijs Cadier

Thijs is a co-founder of AppSignal who sometimes goes missing for months on end to work on our infrastructure. Makes sure our billions of requests are handled correctly. Holds the award for best drummer in the company.

All articles by Thijs Cadier

More articles

  • Using Scientist to Refactor Critical Ruby on Rails Code
  • Bootstrapping with Ruby on Rails Generators and Templates
  • 5 Tips to Design Ruby on Rails Transactions the Right Way
  • The Perils of Parallel Testing in Ruby on Rails
  • Import Maps Under the Hood in Rails 7
  • Delayed Job vs. Sidekiq: Which Is Better?
  • How to Keep Database Table Sizes Down and Prevent Data Bloat
  • Test and Optimize Your Ruby on Rails Database Performance
  • Next Level Ruby on Rails Application Monitoring with AppSignal
  • AppSignal's Top 5 Ruby Posts in 2021

AppSignal monitors your apps

AppSignal provides insights for Ruby, Rails, Elixir, Phoenix, Node.js, Express and many other frameworks and libraries. We are located in beautiful Amsterdam. We love stroopwafels. If you do too, let us know. We might send you some!

Discover AppSignal

AppSignal monitors your apps

shanefrosigh.blogspot.com

Source: https://blog.appsignal.com/2018/06/05/redo-retry-next.html

Post a Comment for "Print Something Then Go Back to Function Then Come Back Again Ruby"