Skip to content

Instantly share code, notes, and snippets.

@challengee
Created December 12, 2012 16:27
Show Gist options
  • Save challengee/4269267 to your computer and use it in GitHub Desktop.
Save challengee/4269267 to your computer and use it in GitHub Desktop.
module Commute
class EnumRouter
include Enumerable
# Initialize the router with a main route.
#
# main - Enumerable
def initialize main
@path = []
self.route main
end
# Push a route on the path (detour).
#
# route - Enumerable
def route route
@path.push route.to_enum
end
# Figure out the next stop.
def succ
begin
return nil if @path.empty?
@path.last.next
rescue StopIteration
@path.pop
retry
end
end
# Implement Enumerable.
def each
yield succ while succ = self.succ
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment