Last active
September 26, 2016 20:55
-
-
Save toretore/71dda01f57ba5444d38a069ff7b549c7 to your computer and use it in GitHub Desktop.
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 'thread' | |
state = {time: Time.now, status: "Flux Capacitor running"} | |
messages = Queue.new | |
class TimeChange | |
attr_reader :time | |
def initialize(t) | |
@time = t | |
end | |
end | |
class StatusChange | |
attr_reader :status | |
def initialize(s) | |
@status = s | |
end | |
end | |
Thread.new do | |
loop do | |
sleep 1 | |
messages << TimeChange.new(Time.now) | |
end | |
end | |
Thread.new do | |
loop do | |
sleep rand(5) | |
messages << StatusChange.new(["Central core overheating", "Proton Generator functionality nominal", "Beryllium capacity low"][rand(3)]) | |
end | |
end | |
def update(message, state) | |
case message | |
when TimeChange then state.merge(time: message.time) | |
when StatusChange then state.merge(status: message.status) | |
end | |
end | |
def render(state) | |
"#{state[:time].strftime('%H:%M:%S')}: #{state[:status]}" | |
end | |
loop do | |
message = messages.pop | |
state = update message, state | |
puts render(state) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment