1. Computing

Click Blocks and Animation


4 of 5

Making a Timer Application
Click Blocks and Animation

Now that you know how to use the animate method, you can create a crude timer application. A timer application has just 3 elements: the time display, a pause button and a reset button. The timer display itself is only text that changes every second, and the pause and reset buttons are two simple buttons.

Here, there are two important internal variables: the number of seconds counted off by the timer and whether or not the timer is paused. The animate block simply increments the number of seconds unless, of course, the timer is paused.

A good chunk of the code is the display_time method. The calculations in this method are converting the number of seconds into hours, minutes and seconds to be displayed in the text. A format string is used, with the format %02d meaning an integer which is, at most, 2 digits long and is "zero padded." Take the number 4 as an example of what this means. Formatting the number 4 in this way will format it as 04.

  1. About.com
  2. Computing
  3. Ruby
  4. Graphical User Interfaces
  5. Shoes
  6. Making a Timer Application

©2014 About.com. All rights reserved.