How to solve Ruby Wizardry “Staying in the loop” example program?

The book Ruby Wizardry Chapter 4 includes the following sample program

we_wanna_ride = true
stops = ["East Bumpspark", "Endertromb Avenue", "New Mixico", "Mal Abochny"]

while we_wanna_ride
    print "Where ya headin', friend?"
    destination = gets.chomp
    if stops.include? destination
    puts "I know how to get to #{destination}! Here's the station list:"
        stops.each do |stop|
            puts stop
            break if stop == destination
        end
    else
        puts "Sorry, we don't stop at that station. Maybe another time!"
        we_wanna_ride = false
    end
end

It then goes on to pose a few additional challenges:

“What if a passenger is going the other way on the train (for instance, from Mal Abochny to East Bumpspark)? How could you update your program to work in both directions? Even trickier, what if the train route is a big circle (meaning if a passenger goes from East Bumpspark to Mal Abochny, the next stop after Mal Abochny should be East Bumpspark again)? How could you update your program to print out the right list of train stops if a passenger wants to go all the way around the circle?”

Does anybody have any ideas how to proceed here ? I’m a beginning programmer so any help would be greatly appreciated. Cheers

1 Like