Introduction to state machines

Yesterday I talked about state machines on the Berlin Ruby Usergroup.

Introduction to state machines

Rails has this nice little feature called Enums. The introduction example is something like this:

class Conversation < ActiveRecord::Base
  enum status: [ :active, :archived ]

And I think this is dangerous. States should be dealed with in a state machine. Why you ask? Because state changes usually have conditions attached to them. Only archive if … . If you want to model something like that with enums, you end up with a horrible version of a state machine.

So let’s see how we would do this in a “more cleaner way” with state machines.

Portrait photo of Bodo Tasche
Bodo Tasche
Polyglot Developer

I am a freelance polyglot developer and love HTML5, testing, JavaScript, Ruby and Elixir. In the last 20 years I have been in lots of different roles, from Java to Elixir, from backend developer at a 3 people team in an early phase startup to the CTO of a web agency. Some of my work can be seen on my projects page.

Need help developing your MVP or to add new features into your current app? Need a CTO or a front/backend developer for hire? Send me an email.