Skip to content

Instantly share code, notes, and snippets.

@toretore
Last active September 26, 2016 20:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toretore/71dda01f57ba5444d38a069ff7b549c7 to your computer and use it in GitHub Desktop.
Save toretore/71dda01f57ba5444d38a069ff7b549c7 to your computer and use it in GitHub Desktop.
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