Created
December 15, 2011 09:05
-
-
Save jihao/1480438 to your computer and use it in GitHub Desktop.
ruby.observer.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "observer" | |
class Tick | |
include Observable | |
def tick | |
loop do | |
now = Time.now | |
changed | |
notify_observers(now.hour, now.min, now.sec) | |
sleep 1.0 - Time.now.usec / 1000000.0 | |
end | |
end | |
end | |
class TextClock | |
def update(h,m,s) | |
printf "%02d:%02d:%02d \n", h, m, s | |
STDOUT.flush | |
end | |
end | |
tick = Tick.new | |
tick.add_observer(TextClock.new) | |
tick.tick |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment