Skip to content

Instantly share code, notes, and snippets.

@itchy
Created December 4, 2012 17:32
Show Gist options
  • Save itchy/4206610 to your computer and use it in GitHub Desktop.
Save itchy/4206610 to your computer and use it in GitHub Desktop.
Best 2012 (b)
valid_routes = add_segments(current_stops, starting_routes(start), stop) { |current, open_routes, matching_routes| current >= max_stops }
def add_segments(current_stops, routes, stop, matching_routes = [], &block )
# This method expects a block that receives the parameters current_stops, open_routes and matching_routes
# When the passed block evaluates to true
# it returns all of the routes found matching the start and stop locations
matching_routes << routes.select { |route| route.last_stop == stop }
matching_routes.compress!
open_routes = routes.select { |route| route.last_stop != stop }
open_routes.compress!
# Return all matches once we have satisfied our block
if open_routes && !open_routes.empty? && matching_routes && !matching_routes.empty?
return matching_routes if yield current_stops, open_routes, matching_routes
end
# recursive call
add_segments(current_stops + 1, next_routes(routes), stop, matching_routes, &block)
end
@itchy
Copy link
Author

itchy commented Dec 4, 2012

Again, I like this snippet of code because it represents a transition in my thinking as it is the first time I created real code that accepts a block as a parameter

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment