0
0
Rubyprogramming~10 mins

Loop method for infinite loops in Ruby - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create an infinite loop using the loop method.

Ruby
loop do
  puts "Hello, world!"
  [1]
end
Drag options to blanks, or click blank then click option'
Abreak
Bexit
Credo
Dnext
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'break' will stop the loop immediately.
Using 'exit' will stop the entire program.
2fill in blank
medium

Complete the code to print numbers from 1 to 5 inside an infinite loop, then break the loop.

Ruby
i = 1
loop do
  puts i
  i += 1
  [1]
end
Drag options to blanks, or click blank then click option'
Aredo if i > 5
Bnext if i > 5
Cbreak if i > 5
Dexit if i > 5
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'next' will skip the rest of the iteration but not stop the loop.
Using 'exit' will stop the whole program, not just the loop.
3fill in blank
hard

Fix the error in the code to create an infinite loop that prints 'Looping'.

Ruby
loop [1]
  puts 'Looping'
end
Drag options to blanks, or click blank then click option'
Awhile true do
Bdo
Cuntil false do
Dfor
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'while true do' after 'loop' is incorrect syntax.
Using 'for' is not valid with 'loop'.
4fill in blank
hard

Fill both blanks to create an infinite loop that prints 'Tick' every second.

Ruby
loop [1]
  puts 'Tick'
  sleep [2]
end
Drag options to blanks, or click blank then click option'
Ado
Bwhile true do
C1
D0.5
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'while true do' after 'loop' is incorrect syntax.
Using 'sleep 0.5' will pause for half a second, not one second.
5fill in blank
hard

Fill all three blanks to create an infinite loop that counts from 1 upwards and breaks after 10.

Ruby
count = [1]
loop [2]
  puts count
  count += [3]
  break if count > 10
end
Drag options to blanks, or click blank then click option'
A0
Bdo
C1
D10
Attempts:
3 left
💡 Hint
Common Mistakes
Starting count at 1 will print 1 to 11 instead of 1 to 10.
Using 'while true do' instead of 'do' after 'loop' is incorrect.