On Wednesday 03 November 2004 11:34 am, Eivind Eklund wrote:
> Wouldn't this be better implemented as meta-programming in Ruby,
Funny you should mention this ... I was playing with ruby state machines about
two weeks ago (even using the turnstile example in my unit tests). This is
what I came up with ....
class TurnStileFSM < StateMachine
state_machine do
state :locked do
start_state
event :coin, :unlocked, :unlock
event :pass, :violation, :alarm_on
end
state :unlocked do
event :coin, :unlocked, :thank_you
event :pass, :locked, :lock
end
state :violation do
event :reset, :violation
event :ready, :locked, :alarm_off, :lock
event :pass
event :coin
end
end
end
To use, you just inherit from the FSM and implement the actions (e.g. unlock,
lock, alarm_on). (Or I suppose you could implement the actions directly in
the FSM class).
--
-- Jim Weirich jim / weirichhouse.org http://onestepback.org
-----------------------------------------------------------------
"Beware of bugs in the above code; I have only proved it correct,
not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)