rewrote automata class to ruby