Skip to content

Instantly share code, notes, and snippets.

@JoergWMittag
Created January 21, 2009 05:49
Show Gist options
  • Save JoergWMittag/49866 to your computer and use it in GitHub Desktop.
Save JoergWMittag/49866 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'fiber'
# This state machine corresponds to the regular expression /ab*(c|d)/
@start = Fiber.new do
loop do
case (@string.next rescue nil)
when 'a' then @state1.transfer
else @errorstate.transfer
end
end
end
@state1 = Fiber.new do
loop do
case (@string.next rescue nil)
when 'b' then @state1.transfer
when 'c', 'd' then @endstate.transfer
else @errorstate.transfer
end
end
end
@endstate = Fiber.new do
puts "It's a match!"
end
@errorstate = Fiber.new do
raise ArgumentError, 'String does not match!'
end
@string = 'abbbbb'.chars
@start.transfer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment