Skip to content

Instantly share code, notes, and snippets.

@jdblack
Created September 7, 2014 05:59
Show Gist options
  • Save jdblack/d3401098c44c37baee82 to your computer and use it in GitHub Desktop.
Save jdblack/d3401098c44c37baee82 to your computer and use it in GitHub Desktop.
require 'finite_machine'
require 'pp'
class StopLight < FiniteMachine::Definition
events {
initial :red
event :power_on, :off => :red
# event :power_off, :any => :off
event :power_off, [:on, :red, :green, :yellow] => :off
event :change, :red => :green
event :change, :green => :yellow
event :change, :yellow => :red
}
callbacks {
on_transition do |event|
pp event
end
}
end
sl = StopLight.new
loop do
puts "***Change"
sl.change
if rand(5) == 0
puts "*** Whoops. Lost power!"
sl.power_off
end
sleep 1
puts
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment