Skip to content

Instantly share code, notes, and snippets.

@toretore
Last active Sep 26, 2016
Embed
What would you like to do?
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