Today I Learned

hashrocket A Hashrocket project

Ruby Redo

Ruby has a keyword redo that you might see inside a block. It causes the unconditional re-execution of the block, with the same parameter bindings as the current execution.

Here it is, causing an endless loop:

n = 0
until n == 1 do
  puts n
  n += 1

redo does not evaluate the 'until' condition, so the likelihood of it causing an endless loop is high.

I'd love to see a practical application. It seems to me that redo would have to be inside a conditional, based on some information coming from outside the loop-- information we are certain will eventually be false.

That's a lot to ask; I would avoid redo in most cases.

See More #ruby TILs
Looking for help? Each developer at Hashrocket has years of experience working with Ruby applications of all types and sizes. We're an active presence at Ruby conferences, have written some of the most popular gems, and have worked on many of the web's Ruby on Rails success stories. Contact us today to talk about your Ruby project.