From: Ara.T.Howard@... Date: 2004-11-06T09:53:41+09:00 Subject: Re: Threaded Observer On Sat, 6 Nov 2004, James Edward Gray II wrote: > Help Thread gurus! :D > > Why is the following code not printing the time every three second the > "Clock" is running, as I believe it should be? > > #!/usr/bin/env ruby > > require "observer" > require "singleton" > > class Clock > include Observable > include Singleton > > def start( seconds_delay ) > @thread = Thread.new(self, seconds_delay) do |notifier, delay| > loop do > puts "Notifying..." > notifier.notify_observers > sleep delay > end > end > end > > def stop > unless @thread.nil? > @thread.kill > @thread = nil > end > end > end > > if $0 == __FILE__ > class TimePrinter > def update > puts Time.now > end > end > > clock = Clock.instance > clock.add_observer TimePrinter.new > > puts "Starting clock..." > clock.start 3 > sleep 10 > clock.stop > puts "Clock stopped..." > sleep 10 > end > > __END__ > > Thanks. > > James Edward Gray II it does for me. on linux. are you on windows? if so the IO (puts) is probably causing the whole process to sleep. regards. -a -- =============================================================================== | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov | PHONE :: 303.497.6469 | When you do something, you should burn yourself completely, like a good | bonfire, leaving no trace of yourself. --Shunryu Suzuki ===============================================================================