Skip to content

Instantly share code, notes, and snippets.

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 dcadenas/625522 to your computer and use it in GitHub Desktop.
Save dcadenas/625522 to your computer and use it in GitHub Desktop.
require 'state_pattern'
class Stop < StatePattern::State
def next
sleep 3
transition_to(Go)
end
def color
"Red"
end
end
class Go < StatePattern::State
def next
sleep 2
transition_to(Caution)
end
def color
"Green"
end
end
class Caution < StatePattern::State
def next
sleep 1
transition_to(Stop)
end
def color
"Amber"
end
end
class TrafficSemaphore
include StatePattern
set_initial_state Stop
end
semaphore = TrafficSemaphore.new
loop do
puts semaphore.color
semaphore.next
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment