Created
August 23, 2013 17:36
-
-
Save radavis/6321931 to your computer and use it in GitHub Desktop.
The Next Train assignment.
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
EASTEREGG = "journey.txt" | |
class TrainSchedule | |
FILENAME = 'schedule.txt' | |
@@schedule = {} | |
def initialize | |
if @@schedule.empty? | |
File.open(FILENAME, 'r').each do |line| | |
train, time = line.split(",") | |
@@schedule[train] = time.to_f | |
end | |
end | |
end | |
def next(now) | |
@@schedule.select { |train, time| time > now }.first | |
end | |
end | |
trains = TrainSchedule.new | |
puts "What time are you leaving? " | |
print "> " | |
leaving_time = gets.chomp.to_f | |
train, time = trains.next(leaving_time) | |
puts "You should catch #{train} leaving at #{time}\n" | |
# journey easter egg | |
if time == 24 | |
File.open(EASTEREGG, 'r').each { |line| puts line } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment