Why do we needs act_as_state_machine?

I kind of new at this…please correct me if I’m wrong.
state_machine do
state :red
state :green
state :yellow

---------------a example---------------
event :change_color do
transitions :to => :red, :from => [:yellow], :on_transition =>
:catch_runners
transitions :to => :yellow, :from => [:green]
transitions :to => :green, :from => [:red]
end
end

it turn yellow to red and trigger catch_runners action. we can use

if state == yellow
state = red
catch_runner
end

instead of it.
so why do we needs act_as_state_machine? a wrapper ? make thing simple?

Zhenning G. wrote:


if state == yellow
state = red
catch_runner
end

instead of it.
so why do we needs act_as_state_machine? a wrapper ? make thing simple?

You are actually illustrating the point to state machines. Yes, in this
simple example you can implement them using a series of conditionals,
but not every state machine is this simple. You could end up with a lot
of conditionals and the code could end up being difficult to follow.

Good design is not always about producing the fewest number of lines.
Sometimes a more verbose and descriptive code can improve
maintainability.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs