From: Carlos Date: 2001-11-22T22:01:29+09:00 Subject: [ruby-talk:26133] Re: Community spike: Fast-state-machines/goto challenge > What's your best ideas for how to implement this? Start brainstorming; > when/if we get a fairly complete list I'll benchmark the ideas and come > back with the figures. Or you can write code yourself. This can be fun! Here is a simple straightforward approach, from a person who doesn't have any expertise in the subject. You can use it to test how much faster the others are ;). Btw, your test program failed with my rubyunit 0.4.6. I had to change initialize to def initialize (a,b) super(a,b) [etc...] end Here's my class: class StateMachine def initialize (start_state, definition) # flatten definition @states=[] definition.each do |d| id_state = d[0] state_def = {} d[1] && d[1].each do |token,next_state| if Array===token token.each do |t| state_def[t]=next_state end else state_def[token]=next_state end end @states << [id_state, state_def] end # convert id_states into references @states.each do |st| st[1].each_key do |k| st[1][k] = get_state(st[1][k]) end end @state=get_state(start_state) end def get_state (id_state) @states.detect { |st| st[0] == id_state } end private :get_state def states @states.collect { |st| st[0] } end def state @state[0] end def run (program) program.each do |token| @state=@state[1][token] end end end