Recall & Review
beginner
What does the
loop method do in Ruby?The
loop method runs the block of code inside it repeatedly forever, creating an infinite loop unless stopped with a break.Click to reveal answer
beginner
How do you stop a
loop in Ruby?You stop a
loop by using the break keyword inside the block when a certain condition is met.Click to reveal answer
beginner
Example: What will this code do?<br>
loop do puts "Hello" break end
It will print "Hello" once and then stop because
break ends the loop immediately.Click to reveal answer
intermediate
Why use
loop instead of while true for infinite loops?loop is a Ruby method designed for infinite loops and can be more readable and expressive. It also uses a block which can be clearer to write and understand.Click to reveal answer
beginner
What happens if you forget to use
break inside a loop?The program will keep running the loop forever, which can freeze or crash your program if not handled properly.
Click to reveal answer
What keyword stops a
loop in Ruby?✗ Incorrect
The
break keyword immediately exits the loop.What does this code print?<br>
loop do puts "Hi" break end
✗ Incorrect
The loop prints "Hi" once and then breaks out of the loop.
Which of these is a correct way to create an infinite loop in Ruby?
✗ Incorrect
loop do ... end runs forever unless broken.What will happen if you run
loop do puts 'Hello' end without a break?✗ Incorrect
Without
break, the loop runs forever printing 'Hello'.Why might you prefer
loop do over while true?✗ Incorrect
loop is a Ruby method designed for infinite loops and is often clearer.Explain how the
loop method works in Ruby and how you can stop it.Think about what happens inside the block and how to exit.
You got /3 concepts.
Describe a situation where using
loop do ... end is helpful and how to avoid problems with infinite loops.Consider real-life tasks that repeat until a condition is met.
You got /4 concepts.