How to create a countdown timer in Ruby

Learning to build a countdown timer in Ruby helps you practice essential programming skills like time manipulation, loops, and user input handling. This foundational project is useful in various real-world applications, such as task schedulers, reminders, games, and productivity tools. By mastering this, you can create more complex, time-based features in your applications, making it a valuable exercise for both beginners and experienced developers.

Creating the script

Shown below is the timer.rb file with the following code:

def clear_screen
    # Check if the TERM environment variable is set before clearing the screen
    if ENV['TERM']
      system('clear') || system('cls')
    end
  end
  
  def countdown_timer(seconds)
    while seconds > 0
      hours, remainder = seconds.divmod(3600)
      mins, secs = remainder.divmod(60)
      timeformat = format('%02d:%02d:%02d', hours, mins, secs)
      
      print "\r#{timeformat}    "  # Add extra spaces to clear any remnants
      sleep(1)
      seconds -= 1
  
      clear_screen
    end
  
    puts "\nTime's up!"
  end
  
  # Set the countdown time in seconds
  countdown_time = 30  # Change this to the desired countdown time
  
  countdown_timer(countdown_time)
  
Working example of a countdown timer in Ruby

Code explanation

  • Line 1: We define a clear_screen function that calls the system('clear') or system('cls') method depending on the operating system to clear the screen’s output after every second so that only one line is shown on the screen at any time.

  • Line 9: The main logic of the countdown timer is in the countdown_timer function. Here, we create a while loop that repeatedly decrements the timer count until it reaches 0.

  • Lines 10–11: The divmod function returns the quotient and remainder when the seconds variable is divided by 3600. Since one hour contains 3600 seconds, the quotient of the division will be the number of hours present in seconds. The remainder is then also divided by 60 using the divmod function to get the number of minutes and seconds in the remainder.

  • Line 12: We use the hours, mins, and secs to create a formatted string, timeformat. This will display time in a timer format, HH:MM:SS, where HH represents the number of hours, MM represents the number of minutes and SS represents the number of seconds. The %02d is used to format the hours, mins, and secs to two digits.

  • Lines 14–15: We print the timer count to the screen as soon as the timeformat string is ready. We use the sleep function with an argument of 1 to simulate the flow of time. After every second, the timeformat string will be updated to display the updated timer count.

  • Lines 16–18: After one second, the seconds variable is decremented by 1 to calculate the hours, minutes, and seconds in the remaining timer count. The clear_screen function is used to clear the screen output so that only one line shows the timer count on the screen.

  • Line 21: Finally, we print a string to the screen after the timer count reaches 0 to indicate the timer has expired.

  • Line 27: We call the countdown timer and pass the number of seconds as an argument to start the timer from that time.

Conclusion

In this answer, we just print a statement after the timer expires, but there are many possibilities, such as calling a backup script that will start a backup of data after a certain time or calling the shutdown script to shut down a system after a certain period of user inactivity.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved