rubyrhodesstopwatch

how to invoke a method for every second in ruby


I wanted to create a stopwatch program in ruby so I googled it and found this SO Q.

But over there, the author calls the tick function with 1000xxx.times. I wanted to know how I can do it using something like (every second).times or for each increment of second do call the tick function.


Solution

  • Thread.new do
      while true do
        puts Time.now # or call tick function
        sleep 1
      end
    end