[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Threaded Observer

James Gray

11/5/2004 11:18:00 PM

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



2 Answers

James Gray

11/5/2004 11:24:00 PM

0

On Nov 5, 2004, at 5:18 PM, James Edward Gray II wrote:

> Help Thread gurus! :D

Figures, I got it as soon as I pushed "Send". The correction is below,
for the curious.

Sorry about the noise.

James Edward Gray II

> #!/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..."

changed # I often forget this, for some reason.

> 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
>
>
>



Ara.T.Howard

11/6/2004 12:49:00 AM

0