Created
December 12, 2012 16:27
-
-
Save challengee/4269267 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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