Skip to content

Instantly share code, notes, and snippets.

@thoolihan
Created April 23, 2014 21:26
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 thoolihan/11233024 to your computer and use it in GitHub Desktop.
Save thoolihan/11233024 to your computer and use it in GitHub Desktop.
StateMachine Example
require 'rubygems'
require 'statemachine'
class MowerContext
def cut_mode
puts "blade start spinning"
end
def idle_mode
puts "engine idling"
end
def stop_mode
puts "completely stopped"
end
def chug
puts "chugging to start"
end
end
mower1 = Statemachine.build do
state :stopped do
on_entry :stop_mode
event :key, :running
on_exit :chug
end
state :running do
on_entry :idle_mode
event :key, :stopped
event :handle, :cutting
end
state :cutting do
on_entry :cut_mode
event :key, :stopped
event :handle, :running
end
context MowerContext.new
end
puts "===first use==="
puts "turning key"
mower1.key
puts "using handle"
mower1.handle
puts "using handle"
mower1.handle
puts "turning key"
mower1.key
puts "\n===another day==="
puts "turning key"
mower1.key
puts "using handle"
mower1.handle
puts "turning key"
mower1.key
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment